[Numpy-discussion] finding nonzero elements in list
Gökhan Sever
gokhansever at gmail.com
Mon Oct 12 10:39:59 EDT 2009
On Mon, Oct 12, 2009 at 9:18 AM, per freem <perfreem at gmail.com> wrote:
> hi all,
>
> i'm trying to find nonzero elements in an array, as follows:
>
> a = array([[1, 0],
> [1, 1],
> [1, 1],
> [0, 1]])
>
> i want to find all elements that are [1,1]. i tried: nonzero(a ==
> [1,0]) but i cannot interpret the output. the output i get is:
> (array([0, 0, 1, 2]), array([0, 1, 0, 0]))
>
> i simply want to find the indices of the elements that equal [1,0].
> how can i do this? thanks.
>
You might simply apply a mask to your array satisfying the condition:
I[1]: a = array([[1, 0],
...: [1, 1],
...: [1, 1],
...: [0, 1]])
I[2]: a == [1,0]
O[2]:
array([[ True, True],
[ True, False],
[ True, False],
[False, False]], dtype=bool)
I[3]: a[a==[1,0]]
O[3]: array([1, 0, 1, 1])
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
--
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20091012/35fa4927/attachment.html>
More information about the NumPy-Discussion
mailing list