[Numpy-discussion] Are there command similar as Matlab find command?
Travis E. Oliphant
oliphant@enthought....
Mon Sep 29 16:48:23 CDT 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