[SciPy-user] syntax for indexing

Angus McMorland amcmorl@gmail....
Fri Nov 7 11:47:40 CST 2008


2008/11/7 Michael Hearne <mhearne@usgs.gov>:
> 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?

Very close. Because of their place in the evaluation order, logical
operators need to be separated by brackets.

i = ((x > 0.5) & (x < 0.8)).nonzero()

should do what you want.

HTH,

Angus.
-- 
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh


More information about the SciPy-user mailing list