[Matrix-SIG] Yet another newbie question

Just van Rossum just@letterror.com
Thu, 23 Apr 1998 14:59:09 +0200


Hullo,

First of all, thank you for your previous help, especially David. I am now
successfully transforming vector arrays at an incredible speed, with an
incredible ease! I am in awe.

Anyway, I have one more question. I want to quickly find the indices of
array elements that are true for a certain expression/condition. More
specifically: I have an array containing two dimensional coordinates, and
I'd like to find the indices of those coordinates that are within a
specified rectangle.
I have the feeling that Numeric.where() might do something I could use, but
I don't understand the explanations in either the official doc nor in
David's tutorial :-(.

... a while later... I think I understand it slightly better now.
where() that is. Still, what I've just coded up feels clumsy:

from Numeric import *
# some coordinates
a = array([[ 163.,  -28.],
       [ 152.,  -28.],
       [ 117.,  -23.],
       [  88.,    0.],
       [  34.,   43.],
       [  16.,  123.],
       [  16.,  180.],
       [  16.,  201.],
       [  18.,  217.],
       [  21.,  227.]],'f')
# the rectangle I'd like to match
lefttop = [30, 40]
rightbottom = [40, 50]

condition = logical_and(greater(a, lefttop), less(a, rightbottom))
w = where(condition, 1, 0)
print w

Now w is an array containing [0, 0] pairs for mismatches, and [1, 1] for
matches. But I still have to iterate to get to the indices...
Is there something more convenient/efficient?

Thanks in advance for any tips.

Just