# [Numpy-discussion] Finding indices and setting values

Todd Miller jmiller at stsci.edu
Wed May 21 09:01:14 EDT 2003

```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

```