![](https://secure.gravatar.com/avatar/a5885e2535fa08c0a187b34127562335.jpg?s=120&d=mm&r=g)
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. ------------------------------------------------------
![](https://secure.gravatar.com/avatar/9decddc020f244d97b6651302b277c60.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/66cfe7cb676bbd44769ebc394f2ecac9.jpg?s=120&d=mm&r=g)
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.
----------------------------------------------------- Damian Eads Ph.D. Student Jack Baskin School of Engineering, UCSC E2-489 1156 High Street Machine Learning Lab Santa Cruz, CA 95064 http://www.soe.ucsc.edu/~eads
participants (3)
-
Angus McMorland
-
Damian Eads
-
Michael Hearne