filtering numeric arrays

Lukasz Pankowski lupan at
Mon Mar 3 17:57:37 CET 2003

"Marie-Claude Savoie" <marie-claude.savoie at> writes:

> 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

Tested only for your example array :-), but should work:

from Numeric import *

# [(x,y) for x,y in seq if xMin < x <xMax and yMin< y <yMax]

a = array([(1,3),(2,4),(5,6)])

def filter_range(a, xMin, xMax, yMin, yMax):
    cc = greater(a, [xMin, yMin])
    cond = logical_and(cc[:,0], cc[:,1])
    less(a, [xMax, yMax], cc)
    logical_and(cond, cc[:,0], cond)
    logical_and(cond, cc[:,1], cond)
    return compress(cond, a, 0)

print filter_range(a, 0, 3,  3, 10)
print filter_range(a, 0, 7,  3, 10)
print filter_range(a, 0, 3,  0, 10)


=*= Lukasz Pankowski =*=

t o t f s  h i m o f  p h t s s w
h n h i a  o s o f o  r o o a o i
e e a r i  p   t   o  o p   y m s
    t s d  e   h   l  b e     e e
      t        e   s  a d     t
               r      b       h
                      l       i
                      y       n

More information about the Python-list mailing list