Hi All, I have an 2D numeric array of x,y points eg [(1,3),(2,4),(5,6)] and I would like to remove all the points in the array that don't meet the min/max point criteria. I will have several thousand points. With lists I can do it like [(x,y) for x,y in seq if xMin < x <xMax and yMin< y <yMax] How do I get the same functionality and better speed using numeric. I have tried a bunch of things using compress and take but I am running up against a brick wall. Any ideas? Thanks Gordon Williams
Gordon Williams wrote:
Hi All,
I have an 2D numeric array of x,y points eg [(1,3),(2,4),(5,6)] and I would like to remove all the points in the array that don't meet the min/max point criteria. I will have several thousand points. With lists I can do it like
[(x,y) for x,y in seq if xMin < x <xMax and yMin< y <yMax]
How do I get the same functionality and better speed using numeric. I have tried a bunch of things using compress and take but I am running up against a brick wall.
I think you want something like this:
cond = (xMin < a[:,0]) & (a[:,0] < xMax) & (yMin < a[:,1]) & (a[:,1] < yMax) np.compress(cond, a, 0)
Where 'a' is your original Nx2 array. Unfortunately the obvious notation and prettier notation using (xMin < a[:,0] < xMax) fails because python treats that as "(xMin < a[:,0]) and (a[:,0] < xMax)" and "and" is not what you need here, '&' is. -tim
Any ideas?
Thanks
Gordon Williams
------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
On Thursday, February 27, 2003, at 11:05 AM, Gordon Williams wrote:
I have an 2D numeric array of x,y points eg [(1,3),(2,4),(5,6)] and I would like to remove all the points in the array that don't meet the min/max point criteria. I will have several thousand points. With lists I can do it like
This should do it:
a array([[1, 3], [2, 4], [5, 6]])
valid = (a[:,0] > minX) & (a[:,0] < maxX) & (a[:,1] > minY) & (a[:,1] < maxY) take(a,nonzero(valid)) array([ [2, 4]])
Note that & is a bitwise-and, not a logical and, but in this case, the result is the same. Unfortunately, the way Python works, overloading "and" is difficult. -Chris Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (3)
-
Chris Barker
-
Gordon Williams
-
Tim Hochberg