[Numpy-discussion] Getting indices from numpy array withcondition

Nicolas ROUX nicolas.roux at st.com
Tue Nov 18 05:49:30 EST 2008


Thanks ;-) 
It's pretty good, as I can apply it to any condition.



Cheers, 
Nivolas.
-----Original Message-----
From: numpy-discussion-bounces at scipy.org
[mailto:numpy-discussion-bounces at scipy.org] On Behalf Of Francesc Alted
Sent: Tuesday, November 18, 2008 11:40 AM
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] Getting indices from numpy array
withcondition

A Tuesday 18 November 2008, Nicolas ROUX escrigué:
> Hi,
>
> Maybe this is not so clever, but I can't find it in the doc.
> I need to get all indices/index of all occurrences of a value in a
> numpy array
>
>
> As example:
>
> a = numpy.array([1,2,3],[4,5,6],[7,8,9])
> I need to get the indice/index of all array elements where a[a>3]
>
> Any fast/easy way to write this ?

Perhaps the nonzero() method is what you are looking for:

In [9]: (a > 3).nonzero()
Out[9]: (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))

Cheers,

-- 
Francesc Alted
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list