FOR loop question [message #12319] |
Mon, 13 July 1998 00:00 |
lbryan
Messages: 4 Registered: June 1998
|
Junior Member |
|
|
Hello all,
I'm developing a developing a bottom / target detector for a ocean
LIDAR system and found a puzzle with a FOR loop that I don't
understand. Could someone tell me why this code runs:
=======================================================
range = 360
x = findgen(range)*!pi/180
y = sin(x) ;simulates my real data
minarray = where(y eq max(y))
maxpos = minarray(0)
window,0
plot,x,y
for rg = 0,range - 2 do begin
if (rg gt 0) then oplot,x,y(0:rg), psym = 2
if (y(rg) gt 0 and y(rg+1) lt 0) then begin $
bottdepth = float(x(rg))+!pi/180*y(rg)/(y(rg+1) + y(rg))
print,rg
rg = range ;end loop through range
endif else bottdepth = 0
endfor
print,bottdepth
end
========================================================
and this code doesn't:
========================================================
range = 360
x = findgen(range)*!pi/180
y = sin(x)
minarray = where(y eq max(y))
maxpos = minarray(0)
window,0
plot,x,y
for rg = maxpos,range - 2 do begin ;<===only difference is here
if (rg gt 0) then oplot,x,y(0:rg), psym = 2
if (y(rg) gt 0 and y(rg+1) lt 0) then begin $
bottdepth = float(x(rg))+!pi/180*y(rg)/(y(rg+1) + y(rg))
print,rg
rg = range ;end loop through range
endif else bottdepth = 0
endfor
print,bottdepth
end
==========================================================
I know changing the index value within a loop isn't slick programming,
but this is how I happened to write it and now I'm curious why one
works and one doesn't.
Lisa Bryan
Arete Associates
Tucson, AZ
lbryan@azstarnet.com
Arete Associates
Tucson, Arizona
|
|
|