Re: for loops for total newbies [message #54792] |
Fri, 13 July 2007 16:42  |
Anna
Messages: 2 Registered: July 2007
|
Junior Member |
|
|
Thank you both! That was really helpful.
I think I'll be having a lot of fun the coming week :)
(And now I've been reminded that I should change the name of the
variables that came from my advisors' code... I really can't imagine
why he named it 'kaas'. Which means 'cheese'.)
Thanks again,
Anna
|
|
|
Re: for loops for total newbies [message #54795 is a reply to message #54792] |
Fri, 13 July 2007 11:45   |
Conor
Messages: 138 Registered: February 2007
|
Senior Member |
|
|
I'm afraid a lot of this you might just have to come up with
yourself. It's hard (for me anyway) to vectorize everything for you
because I'm just not that familiar with your code. I don't know the
dimensions of everything, and I don't really know what you're trying
to accomplish. However, I think I can provide some hints that should
help.
First of all, why do you have a wait inside a for loop??? Even if it
doesn't cost you that much time, it still has no purpose that I can
see.
Second, remember that if an operation doesn't have to be in the for
loop, don't put it in the for loop! So in your inner-most loop you
have these two lines:
k2=(k+1+ max_cor_shift) mod max_cor_shift
k1=(k-1+ max_cor_shift) mod max_cor_shift
However the loop itself is looping over f, and the loop above it is
looping over i, neither of which appear in either of these lines. It
seems to me that these two variables should be constants inside the
loop, so you should move them out of the loop entirely. That probably
won't make a huge difference, but it will help.
Now the important part. It might be very difficult to get rid of both
for loops, but it should be pretty easy to get rid of the innermost
one. The first trick you will have to do is use the dimensions
keyword to max(). You'll want to get rid of the for loop over f, and
instead try something like this:
crtot= fltarr(sz-1,(size(cor_array))[2],(size(cor_array))[3])
crtot(0:sz-3,*,*)= cor_array(0:sz-3,*,*)
meanarr = fltarr((size(cor_array))[2])
j= findgen(1000*max_cor_shift)/1000.0
avgz= fltarr(sz-2)
fit = fltarr(sz-2, max_cor_shift*1000)
k2=(k+1+max_cor_shift) mod max_cor_shift
k1=(k-1+ max_cor_shift) mod max_cor_shift
for i = 0, (size(cor_array))[2]-1 do begin
print, i
maxcrs = max(crtot,ks,dimension=1)
a = crtot(*,i,k2)/2 + crtot(*,i,k1)/2 - maxcrs
b = crtot(*,i,k2)/2 - crtot(*,i,k1)/2 - maxcrs
c = maxcrs
fit = a*(j-k)^2 + b*(j-k) + c
kaas = max(fit, s, dimension=1)
avgz = s/(float(sz)) - 10
meanarr(i)=mean(avgz)
endfor
Now I really doubt the above code will work, but that's essentially
what you want to do. By saying: maxcrs = max(crtot,ks,dimension=1)
you are returning not just the maximum of a row, but the maximum of
every row. Then, you calculate 'a' for every row, 'b' for every row,
'c' for every row, and 'fit' for every row. I'm not sure if I quite
did it right though, because I'm not very sure what the dimensions of
fit are, and whether or not everything matches up. You'll probably
have to adjust the code accordingly. Same with the calculation of
kaas and avgz. Hopefully though, this will give you an idea of what
needs to be done.
Actually, considering that very little is done in the outer loop, you
might be able to get rid of it as well and do the whole thing loop-
less. I'm not really sure though, you might have to play around with
that yourself.
|
|
|
|
Re: for loops for total newbies [message #54874 is a reply to message #54792] |
Mon, 16 July 2007 06:31  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Anna wrote:
> Thank you both! That was really helpful.
>
> I think I'll be having a lot of fun the coming week :)
> (And now I've been reminded that I should change the name of the
> variables that came from my advisors' code... I really can't imagine
> why he named it 'kaas'. Which means 'cheese'.)
Maybe he thought his code was full of holes.... ?
:o)
cheers,
paulv
--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
|
|
|