Re: Array concatenation [message #48960 is a reply to message #42493] |
Wed, 07 June 2006 07:21   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
David Fanning wrote:
> maye writes:
>
>
>> I was searching for an effective way to do this, but can't find anybody
>> writing about what (as usual) seems to be an obvious problem/task to
>> me. :)
>> I don't know how many elements I will collect while scanning a bunch of
>> images, so I want to use array concatenation to collect the values like
>> this:
>> means = [means, currMean]
>> But for this to compile/run properly, I need mean to exist before.
>> If I do a
>> means = 0.
>> before the loop, I will always have a zero-element in the beginning
>> that I don't want at plotting time. Of course I could remove it with
>> means = means[1:*]
>> but not only does this waste resources, it also becomes cumbersome if I
>> collect 20 different data values, for that I ALL have to remove the
>> first value?
>> Surely there must be a better way?
>> Please help me to program IDL efficiently! :)
>
>
> I think most people do something like this:
>
> IF N_Elements(means) EQ 0 THEN means = [currMean] ELSE $
> means = [Temporary(means), currMean]
>
> Or, did you mean something that doesn't involve any coding? :-)
>
> Be aware that if your loop is large, it is much more
> efficient to allocate the memory for your array in chucks
> of say 100 or 1000, and then fill it up, then it is to
> continuously re-define your array like this.
Let me second David's statement. For many points, array concatenation is s..l..o..w. In my
early IDL days I improved the run time of a procedure (to read a 100,000's->millions of
points) from minutes[*] to effectively instantaneous by switching from concatenation to
allocate a block and then reallocate (or trim) extra space as required. Now it takes
longer to output the number of points read rather than reading them.
For a small number of points array concatenation is great, but it does not scale well.
paulv
[*] When I had to read many, many of the same type of files I could walk down the street
to get a coffee before it had finished.
--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
Ph: (301)763-8000 x7748
Fax:(301)763-8545
|
|
|