# [SciPy-user] syntax for indexing

Fri Nov 7 11:48:37 CST 2008

```Hi Michael,

The following gives a mask array of the same size as the original array,

(x > 0.5) * (x < 0.8)

and using the mask array as an index, gives the values that meet the
condition above,

x[(x > 0.5) * (x < 0.8)]

If you want indices to the flat array, you first need to create a flat
view of it.

xr = x.ravel()

Note that xr is not a copy but a view of x with its striding
parameters changed appropriately.

In [23]: np.where((xr > 0.5) * (xr < 0.8))
Out[23]: (array([ 1,  3,  4,  6, 10]),)

I hope this helps.

Damian

On Fri, Nov 7, 2008 at 9:32 AM, Michael Hearne <mhearne@usgs.gov> wrote:
> Assume that I have a setup like this:
>
> from pylab import *
> x = random((4,4))
>
> I know how to get the indices of the values that are (for example),
> greater than 0.5:
> i = (x > 0.5).nonzero()
>
> How do I get the indices for those values in x that are greater than 0.5
> AND less than 0.8?
>
> I tried:
> i = (x > 0.5 && x < 0.8).nonzero()
> i = (x > 0.5 & x < 0.8).nonzero()
> i = (x > 0.5 and x < 0.8).nonzero()
>
> to no avail.  Is this the wrong approach?
>
> For Matlab users, the functionality which I am trying to replicate is:
> x = rand(4,4);
> i = find(x > 0.5 & x < 0.8);
>
> Thanks,
>
> Mike
>
> --
> ------------------------------------------------------
> Michael Hearne
> mhearne@usgs.gov
> (303) 273-8620
> USGS National Earthquake Information Center
> 1711 Illinois St. Golden CO 80401
> Senior Software Engineer
> Synergetics, Inc.

-----------------------------------------------------
Jack Baskin School of Engineering, UCSC        E2-489
1156 High Street                 Machine Learning Lab
```