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?
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.