Re: Calculating difference between each element in array from previous [message #83671] |
Fri, 29 March 2013 15:07  |
Russell[1]
Messages: 101 Registered: August 2011
|
Senior Member |
|
|
Just a piece of friendly advice. I don't think you're going to get a lot of help, because no one is going to go through your code, line-by-line and fix it for you (just like in your other post). Not only is this incredibly tedious (for the person answering your question, who has nothing to gain by doing this) but nearly impossible to fully diagnose without access to the data. For example, I assume adatea is a string of some sort that somehow encodes the data (such as 20130329 for today), but that's just a guess.
You'll be better served if you ask high-level questions. Such as, I have these two arrays of dates: here are short segments of the arrays:
date1=[20000101,20010101]
date2=[20000102,20000101]
and I want to do the following, preferably without a loop (because eventually the arrays will be of order 10^3 elements long). Or something like this...
Good Luck
Russell
http://www.idlcoyote.com/documents/idl_question.html
On Friday, March 29, 2013 10:43:29 AM UTC-4, morganls...@gmail.com wrote:
> Hello,
>
>
>
> I'm trying to rewrite my code to eliminate for loops as much as possible. I believe I have to have one to start because I'm trying to match a list of dates in one array to a list of dates in another.
>
>
>
> for k=0,n_elements(mcity)-1 do begin
>
> indexa = where(strmatch(strmid(adatea,0,8),strmid(mdate(k),0,8)) eq 1,num)
>
>
>
> It only seems to work if I go one mdate at a time. Thats fine but I'd like to eliminate the other if statements and for loops within this one to speed up calculations if possible. Once I match the date I need to find out if the array matches a selection of criteria to decide whether to use that array or not.
>
>
>
> if num gt 0 then begin ; If matches then go through profile critera
>
> inda = where(finite(aalta(col,row,*)), count) ; Count number of non-NaN altitudes
>
> if (n_elements(inda) ge 15 ) then begin
>
> dcount=0
>
> diffa = fltarr(50)
>
> for k=0,count-2 do begin
>
> diffa(k) = aalta(col,row,inda(k+1))-aalta(col,row,inda(k))
>
> dcount = dcount+1
>
> endfor
>
> diffa = diffa(0:dcount-1)
>
>
>
> ; Only interested in profiles with altitude differences less than 500 m
>
> if (max(abs(diffa)) lt 500.0) then begin
>
> if (j eq 0) or (j gt 0 and i ne iprev) then begin
>
> jcount = 0
>
> icount = icount + 1
>
> endif else begin
>
> if (j gt 0 and i eq iprev) then begin
>
> jcount = jcount + 1
>
> icount = icount
>
> endif
>
> endelse
>
>
>
> ; Now do calculations...
>
>
>
> endif
>
> endif
>
> endif
>
> endfor
>
>
>
> Is there a way to eliminate the diffa loop... and calculate the differences between each altitude another way?
>
> for k=0,count-2 do begin
>
> diffa(k) = aalta(col,row,inda(k+1))-aalta(col,row,inda(k))
>
> dcount = dcount+1
>
> endfor
>
>
>
> I've been reading online about eliminating for loops and rebin/reform but haven't been able to figure out what would work best if anything. Thanks.
>
>
>
> Sincerely,
>
> Morgan
|
|
|
Re: Calculating difference between each element in array from previous [message #83807 is a reply to message #83671] |
Fri, 29 March 2013 17:07  |
Phillip Bitzer
Messages: 223 Registered: June 2006
|
Senior Member |
|
|
For a start, I think I see where you are differencing adjacent elements of an array. This can be done without looping:
diff = arr - SHIFT(arr, 1)
Note SHIFT will wrap around values, so if that's not important you'll want to "mask" this values somehow.
And then, something like
ind = WHERE(ABS(diff) LE 500., lowCount, NCOMPLEMENT=highCount)
will give you the number of values less than your 500 critical value (lowCount) and greater than 500 (highCount).
I *think* this is what you're asking...
BTW, you should be *very* careful using parentheses to access your array elements. It is much better (safer) to do arr[indices]. There are reasons for this, and a good example of why can be found here:
http://www.idlcoyote.com/misc_tips/imagevarname.php
It is *highly* recommended you get in the habit of putting
compile_opt idl2
in all your routines.
|
|
|