[Numpy-discussion] filtering numeric arrays
Tim Hochberg
tim.hochberg at ieee.org
Thu Feb 27 11:24:07 CST 2003
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 at lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
>
>
More information about the Numpy-discussion
mailing list