1

In MATALAB I can write easily:

ind = find(X, k, 'last')

that returns at most the last k indices corresponding to the nonzero entries of X.

Numpy has the numpy.nonzero function just for the MATALAB find(X), the one parameter version.

Which is numpythonic way to translate find with 3 arguments?

2
  • Check out the Mathesaurus page. (The webpage is awesome and helped me go from Matlab to R). Commented May 7, 2015 at 1:27
  • The numpy find returns a tuple - one array for each dimension. Do the normal indexing on this tuple. Commented May 7, 2015 at 2:02

2 Answers 2

2

Equivalent expression is

# importing numpy as np and
# assign a ndarray to x
ind = np.nonzero(x)[-k:]

Using slicing you replace the use of 'last' argument.

Sign up to request clarification or add additional context in comments.

Comments

1

You will need to apply the last or first k through array slicing of the result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.