# [SciPy-user] Looking for function that returns indicies acc'd to condition

James Analytis james.analytis at physics.ox.ac.uk
Wed Dec 17 21:01:41 CST 2003

```Hey,
Maybe you could also find a similar solution for >1-dimensional arrays by
taking advantage of python lists. This one for 2-d arrays

>>> a
array([[1, 2, 3],
[3, 2, 1],
[3, 1, 4]])
>>> index=[]
>>> for i in range(len(a)):
...     for j in range(len(a[:,])):
...             if a[i,j]>=3:
...                     index.append([i,j])
...             else:
...                     pass
...
>>> print index
[[0, 2], [1, 0], [2, 0], [2, 2]]

You would need as many for loops as there are dimensions. This may prove to be
too slow for you.

James

On Tuesday 16 Dec 2003 11:11 pm, Michael Sorich wrote:
> Hi Gary,
>
> I think nonzero is what you are looking for:
> >>> from Numeric import *
> >>> a = array((1,2,3,4,5))
> >>> print nonzero(a>3)
>
> [3 4]
>
>
> As per numpy manual "nonzero() returns an array containing the indices
> of the elements in a that are nonzero. These indices only make
> sense for 1d arrays, so the function refuses to act on anything else."
>
> Cheers,
>
> Michael Sorich
>
>
> -----Original Message-----
> From: scipy-user-bounces at scipy.net [mailto:scipy-user-bounces at scipy.net]
> On Behalf Of Gary Pajer
> Sent: Wednesday, 17 December 2003 8:57 AM
> To: SciPy Users List
> Subject: [SciPy-user] Looking for function that returns indicies acc'd
> to condition
>
> This function must exist in Numeric or scipy, but I can't find it.
>
> Given an array, return an array (or list) of indicies corresponding to
> elements that satisfy a condition.
>
> I.e.,
> a = array((1,2,3,4,5))
> find_index(a>3)
>
> returns [3,4]
>
> tia,
> gary
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

--
James Analytis
Department of Physics
Clarendon Laboratory, Parks Rd.
Oxford, OX1 3PU
MB: +447876 453 171
WK: +441865 272 221

```