[Numpy-discussion] filtering numeric arrays

Chris Barker Chris.Barker at noaa.gov
Thu Feb 27 11:41:12 CST 2003


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 at noaa.gov





More information about the Numpy-discussion mailing list