[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FOR statement

Eli Beckerman <ebeckerman@cfa.harvard.edu> writes:

> Hey,
> I just tried running a FOR loop in the hopes
> of incrementing the variable "i" by steps of 0.25 as follows:
> radius=fltarr(1000)
> FOR i=0.0, 100.0, 0.25 DO BEGIN
>   radius(i)=i
> And what I end up with is an array that starts
> with the value 0.75 and is incremented by steps of 1.
> I'm following the convention of the FOR statement as
> presented in IDL's online help. What am I doing wrong?!

You appear to be mixing the semantics of indexing with assignment.
What do you think happens when you do radius(0.25) = 0.25, which is
followed by radius(0.5) = 0.5 and radius(0.75) = 0.75?  IDL
automatically converts floating point array indices into integers by
rounding down, so all of the statements I just mentioned affect the
same array element, and 0.75 supercedes because it is last.

You probably want:

FOR i=0.0, 100.0, 0.25 DO radius(i*4)=i

Or better yet:

FOR i=0, 999 do radius(i) = i * 0.25 
(which avoids the ambiguity of indexing by a floating point number)

Or even better:

radius = findgen(400)*0.25
(avoids FOR loops altogether)

Also, why do you have a 1000 element array when you only fill the
first 400 elements?


Craig B. Markwardt, Ph.D.         EMAIL:    craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response