Hi, I have a problem where I want to set values in a 2D array based on conditions I’ve established. The variables are the NA of the system, its wavelength and the correct units on the device I’m modeling. Using these variables I define a variable N. I set up a 512 by 512 array [x,y]. Then I set up a radius , r = x^2 +y^2 and ask it to give me all r’s(to the closest integer) <= N(the variable I’ve defined above). This gives all the index values in that radius and then I set all those locations to a value of 1. After this I do some FFT ‘s, etc. So how do I do this in Numerical Python without having to index through i,j steps which would be incredibly tedious. This works fairly well in MatLab but I want to port my program to Python(for lots of reasons). If you’d rather I write the MatLab code snippet I can do that. Thanks. Cliff Martin
I have a problem where I want to set values in a 2D array based on conditions I’ve established. The variables are the NA of the system, its wavelength and the correct units on the device I’m modeling. Using these variables I define a variable N. I set up a 512 by 512 array [x,y]. Then I set up a radius , r = x^2 +y^2 and ask it to give me all r’s(to the closest integer) <= N(the variable I’ve defined above). This gives all the index values in that radius and then I set all those locations to a value of 1. After this I do some FFT ‘s, etc. So how do I do this in Numerical Python without having to index through i,j steps which would be incredibly tedious. This works fairly well in MatLab but I want to port my program to Python(for lots of reasons). If you’d rather I write the MatLab code snippet I can do that. Thanks.
Cliff Martin
One way should be something like this (I haven't tested this) This only works in numarray. If data is the 2-d array where you want to set values within a radius of N (of x0, y0 I presume) y, x = indices(data.shape) data[nonzero(((x-x0)**2+(y-y0)**2) < N**2)] = 1 (in a future version of numarray this will also work and the meaning should be clearer: data[where(((x-x0)**2+(y-y0)**2) < N**2)] = 1 It's a little more involved for Numeric but still possible to do without resorting to loops (I'll leave it to someone else to show that). Does this do what you wanted? Perry Greenfield.
Perry Greenfield <perry@stsci.edu>: [snip]
(in a future version of numarray this will also work and the meaning should be clearer: data[where(((x-x0)**2+(y-y0)**2) < N**2)] = 1
Cool... Do you know which version? -- Magnus Lie Hetland "In this house we obey the laws of http://hetland.org thermodynamics!" Homer Simpson
On Wed, 2003-05-21 at 11:08, Cliff Martin wrote:
Hi,
I have a problem where I want to set values in a 2D array based on conditions I’ve established. The variables are the NA of the system, its wavelength and the correct units on the device I’m modeling. Using these variables I define a variable N. I set up a 512 by 512 array [x,y]. Then I set up a radius , r = x^2 +y^2 and ask it to give me all r’s(to the closest integer) <= N(the variable I’ve defined above). This gives all the index values in that radius and then I set all those locations to a value of 1. After this I do some FFT ‘s, etc. So how do I do this in Numerical Python without having to index through i,j steps which would be incredibly tedious. This works fairly well in MatLab but I want to port my program to Python(for lots of reasons). If you’d rather I write the MatLab code snippet I can do that. Thanks.
Cliff Martin
I think part of your code looks like this: 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
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
participants (4)
-
Cliff Martin
-
Magnus Lie Hetland
-
Perry Greenfield
-
Todd Miller