Re: FOR loop question [message #12318] |
Mon, 13 July 1998 00:00 |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Lisa Bryan wrote:
>
> 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 [...]
> and this code doesn't [run]:
> ========================================================
> 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
> ==========================================================
Here's the error message:
% Type of FOR statement index variable RG may not be changed.
If you replace your PRINT,RG statement with help,rg you'll see it is
RG LONG = 179
... and if you type
help,rg,range,maxpos
after it died you find:
RG INT = 360
RANGE INT = 360
MAXPOS LONG = 90
(apparently it *did* change the type before it died !)
The WHERE function always returns a LONG integer (would be a shame
otherwise ;-). As a quick fix, you can typecast RANGE before the loop:
range = long(range)
=======
BTW: if you don't need to overplot part of that curve over and over
again, you can speed up your code considerably by replacing the
oplot,x,y(0:rg), psym = 2
by
plots,x(rg),y(rg),psym = 2
And if you are only interested in the bottdepth value, take a look at
the SHIFT function which will shift an array by a specified number of
indices.
test = y * shift(y,-1)
ind = (where(y gt 0. AND test lt 0.))[0]
This returns the same index (179) and you can compute your bottdepth
from here *without* any loop!
Hope this helps,
Martin.
--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/
------------------------------------------------------------ -------
|
|
|