comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Calculating difference between each element in array from previous
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Calculating difference between each element in array from previous [message #83680] Fri, 29 March 2013 07:43 Go to next message
morganlsilverman is currently offline  morganlsilverman
Messages: 46
Registered: February 2013
Member
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 #83805 is a reply to message #83680] Fri, 29 March 2013 19:03 Go to previous message
dg86 is currently offline  dg86
Messages: 118
Registered: September 2012
Senior Member
On Friday, March 29, 2013 8:07:08 PM UTC-4, Phillip Bitzer wrote:
> 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.

How about

diff = arr - arr[1:*]

This addresses the wrap-around issue.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Using where to create new variables of data
Next Topic: Re: Can one close only one of STDIN and STDOUT when using SPAWN with the UNIT keyword?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 03:28:29 PDT 2025

Total time taken to generate the page: 7.60030 seconds