[SciPy-user] How to use where
Chris Lee
c.j.lee at tnw.utwente.nl
Thu Aug 21 07:13:44 EDT 2008
Well what I would normally do is this
listofvectors = ..
t1 = where(listofvectors < xmax, 1, 0)
t2 = where(listofvectors < ymax,1,0)
inBound = t1*t2
rows = nonzero(inBound)[0]
cols = nonzero(inBound)[1]
while someCondition:
listofvectors[rows,cols] = someoperation(listofvectors[rows,cols])
the point is that the nonzero operation will allow you to slice the
listofvectors so that only the elements still in bound are used in
your loop.
Hope this helps
Cheers
Chris
On Aug 21, 2008, at 11:41 AM, Alexander Borghgraef wrote:
> Hi all,
>
> I'm trying to figure out here how 'where' works exactly. I'm working
> on a list of vectors which I represent as a 2D array, and I'd like to
> remove the vectors which are out of bounds. So after some
> experimenting I got to basically this:
>
> listofvectors = ...
> # shape is ( 100, 2 )
> bound = array( [ xmax, ymax ] )
> inside = where( all( listofvectors < bound, axis = 1 ) ) # inside
> is ( array[ 1, 2, 4, 10, ... ] )
> listofvectors = listofvectors[ inside, : ]
> # shape is ( 1, 100, 2 )
>
> Ok, so I'm almost there. The change of shape is annoying, since I'll
> be using listofvectors in a loop to sample from an image. Not that I
> can't work around this, but I'm looking for a cleaner way. Also, I
> don't really get why numpy is doing this, anyone care to explain?
> Thanks.
>
> --
> Alex Borghgraef
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
***************************************************
Chris Lee
Laser Physics and Nonlinear Optics Group
MESA+ Research Institute for Nanotechnology
University of Twente
Phone: ++31 (0)53 489 3968
fax: ++31 (0)53 489 1102
***************************************************
More information about the SciPy-User
mailing list