[Numpy-discussion] Are there command similar as Matlab find command?

Travis E. Oliphant oliphant at enthought.com
Mon Sep 29 17:48:23 EDT 2008


frank wang wrote:
> Hi,
>  
> I am trying to find a command in numpy or python that perform similar 
> function as Matlab find command. It will return the indexes of array 
> that satisfy a condition. So far I have not found anything.

There are several ways to do this, but what are you trying to do?   
Non-zero on the boolean array resulting from the condition is the most 
direct way:

(a>30).nonzero()
where(a>30)

This returns a tuple of indices of length nd, where nd is the number of 
dimensions of a.  (i.e. for 1-d case you need to extract the first 
element of the tuple to get the indices you want).

But, if you are going to use these indices to access elements of the 
array, there are better ways to do that:

a[a>30]
compress(a>30, a)

etc.

-Travis




More information about the NumPy-Discussion mailing list