[Numpy-discussion] shortcut nonzero?

Yuxiang Wang yw5aj at virginia.edu
Tue Feb 25 14:28:11 EST 2014


Hi Alan,

If you are only dealing with 1d array, What about:

np.nonzero(your_array)[0][:k]

?

-Shawn


On Tue, Feb 25, 2014 at 2:20 PM, Alan G Isaac <alan.isaac at gmail.com> wrote:

> Is there a shortcut version for finding the first (k) instance(s) of
> nonzero entries?
> I'm thinking of Matlab's `find(X,k)`:
> http://www.mathworks.com/help/matlab/ref/find.html
> Easy enough to write of course.
>
> I thought `flatnonzero` would be the obvious place for this,
> but it does not have a `first=k` option.
> Is such an option worth suggesting?
>
> Thanks,
> Alan Isaac
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Yuxiang "Shawn" Wang
Gerling Research Lab
University of Virginia
yw5aj at virginia.edu
+1 (434) 284-0836
https://sites.google.com/a/virginia.edu/yw5aj/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140225/adbc954b/attachment.html>


More information about the NumPy-Discussion mailing list