[Numpy-discussion] finding nonzero elements in list

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Oct 12 10:36:09 EDT 2009


On Mon, Oct 12, 2009 at 10: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.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


a == [1,0]  does elementwise comparison, you need to aggregate
condition for all elements of row

>>> a = np.array([[1, 0],
      [1, 1],
      [1, 1],
      [0, 1]])
>>> np.nonzero((a==[1,0]).all(1))
(array([0]),)
>>> np.where((a==[1,0]).all(1))
(array([0]),)
>>> np.nonzero((a==[1,1]).all(1))
(array([1, 2]),)

Josef



More information about the NumPy-Discussion mailing list