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

Home » Public Forums » archive » Re: Avoiding FOR loops ?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Avoiding FOR loops ? [message #40662] Wed, 25 August 2004 08:35
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
Hi Pepe,

"Pepe" <sdj@tiscali.it> wrote in message
news:56bc95a7.0408250231.6a55e2b6@posting.google.com...
> Dear All,
>
> Is there a away to avoid the FOR loops in the following code:
>
> ;no. of months & no. of lat, lon points
> n_months = 256
> lon = 1304
> lat = 1028
>
> ;3-D array with SST observations for all
> ;months and all pixels - Populated
> data_all = fltarr(n_months, lon, lat)
>
> ;Array of all valid observations across all months
> ;for all pixels - To be populated
> data_obs = intarr(lon, lat)
>
> ;Array of mean values across all months
> ;for all pixels - To be populated
> data_mean = fltarr(lon, lat)
>
> ;Array of median values across all months
> ;for all pixels - To be populated
> data_median = fltarr(lon, lat)
>
> ;The values of LAND to be avoided
> ;Bad value obs is defined as 0
> def_val = -9999.9
>
> FOR ilon = 0, lon - 1 DO BEGIN
> FOR ilat = 0, lat - 1 DO BEGIN
> valid = where(data_all(*, ilon, ilat) NE def_val AND $
> data_all(*, ilon, ilat) NE 0, count =
valid_obs)

*** NB: this should be: data_all(*, ilon, ilat) NE 0, valid_obs)
(but we're not going to use it anymore!)

> data_obs(ilon, ilat) = valid_obs
> data_mean(ilon, ilat) = mean(data_all(*, ilon, ilat))
> data_median(ilon, ilat) = median(data_all(*, ilon, ilat))
> ENDFOR
> ENDFOR

I think what you're looking for, instead of the entire FOR loop block,
is:

elementIsValid = data_all NE def_val AND data_all NE 0
data_obs = Total(elementIsValid, 1)

data_mean = Total(data_all, 1) / n_months
data_median = Median(data_all, Dimension=1) ; <- requires IDL 5.6

In fact, with this method, you wouldn't need to declare data_obs,
data_mean and data_median above.

Hope this helps!

Cheers,
--
-Dick

Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
[Message index]
 
Read Message
Previous Topic: Reverse function and degenerate dimensions
Next Topic: Re: Bug with object graphics, app_scroll and the mouse in 6.1 under Windows.

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

Current Time: Wed Oct 08 17:35:29 PDT 2025

Total time taken to generate the page: 0.00387 seconds