[Numpy-discussion] Finding indices and setting values

Perry Greenfield perry at stsci.edu
Wed May 21 14:10:06 CDT 2003


> 
> x,y = Numeric.indices((512,512))
> r = x**2 + y**2
> c = r < N 
> 
> In numarray you can then say:
> 
> a[ c ] = 1
> 
> In Numeric, I think you say:
> 
> a = a.flat
> c = c.flat
> Numeric.put(a, Numeric.nonzero(c), 1)
> a.shape = (512,512)
> 
> 
> Todd
> 
Yeah, that's what I would do in Numeric :-)

I was thinking that the solution I gave was wasteful of memory
if N was generally much smaller than the dimensions of data.
Something like this (also untested) would be better in that
regard (again for numarray)

y, x = indices((2*N+1, 2*N+1))
yind, xind = nonzero(((x-N)**2+(y-N)**2) < N**2) 
data[yind+y0-N, xind+x0-N] = 1

Note that this doesn't check to see if x0 and y0 are at least
N away from the boundaries of data, and also note my convention
for x and y is different than what Todd used (it depends on how
you interpret data in arrays, the convention I use is more common
for image data if you think of x corresponding to the most rapidly
varying index). The advantage of this approach is that the x and y
arrays are small if N is small, and computing xind, yind take much
less time than for very large arrays.

Doing this with Numeric would be much messier I believe (but a clever
person could prove me wrong).

Perry Greenfield





More information about the Numpy-discussion mailing list