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
overcomer
2,4343 gold badges31 silver badges40 bronze badges
2 Answers 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
cosmoscalibur
1,1831 gold badge8 silver badges17 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You will need to apply the last or first k through array slicing of the result.
answered May 7, 2015 at 1:25
Jason Newton
1,22110 silver badges13 bronze badges
Comments
Explore related questions
See similar questions with these tags.
default
MatlabtoR).numpyfind returns atuple- one array for each dimension. Do the normal indexing on this tuple.