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

Home » Public Forums » archive » arithmetic operation on array
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: arithmetic operation on array [message #85478 is a reply to message #85477] Mon, 12 August 2013 14:24 Go to previous messageGo to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Phillip Miller writes:

> Possibly a dumb question, but I'm pretty new to IDL:
>
> I have a geographically explicit time-series with 456 time steps and a
> 1 degree resolution, so an array of dimensions 360 x 180 x 456, and I
> would like to recalculate it as the anomaly from the time-series
> average.
>
> I can calculate the time series average no problem
>
>> average = mean(data, dimension=3)
>
> But, of course, when I try
>
>> anomaly = data - mean(data, dimension=3)
>
> then I "lose" my third dimension, and end up with an array of 360 x
> 180, rather than what I want, which is an array that is the same size
> as my original.
>
> I know that I could loop it like
>
>> for i = 0,456 data[*,*,i] = data[*,*,i] - mean(data, dimension=3)
>
> but I feel like there must be a better way than making a for loop. Am
> I supposed to duplicate mean(data, dimension=3) times 456 in order to
> create an identically sized array for the minus operation? (i.e., an
> array with dimensions 360 x 180 x 456, but where each of the 456
> "slices" is identical)
>
> Thanks in advance for any suggestions!

The answer depends on how big your arrays are, how much on-board memory
your machine has, and whether there is a coffee machine nearby.
Personally, I wouldn't be worrying about optimizing your code until you
discover there is a need to do so.

I wouldn't, however, use code like this:

for i = 0,456 data[*,*,i] = data[*,*,i] - mean(data, dimension=3)

This is guaranteed to be slow, because you are calculating the mean
every time through the loop. Since that doesn't change, calculate it
once:

average = mean(data, dimension=3)
for i = 0,456 data[*,*,i] = data[*,*,i] - average

If you want to give it a try the IDL way, I would try something like
this:

average = mean(data, dimension=3)
data = Temporary(data) - Rebin(average, 360, 180, 456)

You can time it by wrapping the code in the routines TIC and TOC. We
would all be curious to see the results. :-)

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Coyote Graphics PS/PDF output size/orientation
Next Topic: Not IDL Related Question

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

Current Time: Fri Oct 10 15:08:26 PDT 2025

Total time taken to generate the page: 0.71843 seconds