[SciPy-user] How to use where

Alexander Borghgraef alexander.borghgraef.rma@gmail....
Thu Aug 21 09:26:55 CDT 2008


On Thu, Aug 21, 2008 at 4:03 PM, Dave <dave.hirschfeld@gmail.com> wrote:
> In [1]: listofvectors = rand(100,2)
>
> In [2]: bound = array([0.5,0.8])
>
> In [3]: idx = all(listofvectors  < bounds,axis=1)
>
> In [4]: inbounds = listofvectors[idx,:]

Neat. One less function call, seems like a good thing to me.

> I'm not sure of the utility of where - I tend to use boolean masks. Is there any
> reason one wouldn't use the code I posted above?

Can't see why not (please someone correct me if I'm wrong). After
playing with where a bit, my impression is that its main use is the
conditional replacement of elements, and not what I was trying to do
with it. For example:

a = arange(10)
> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9])
where( a > 4, 0, a ** 2)
> array([ 0,  1,  4,  9, 16, 25, 36,  0,  0,  0])

Anyway, thanks to everyone who responded!



-- 
Alex Borghgraef


More information about the SciPy-user mailing list