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?

JasonMArcher
15.2k22 gold badges59 silver badges53 bronze badges
asked May 7, 2015 at 1:16
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.

answered May 7, 2015 at 1:47
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.

answered May 7, 2015 at 1:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.