"Jeremy " <jeremy.brower@asu.edu> wrote in message <htid1t$89l$1@fred.mathworks.com>...
> Hi all. I'd appreciate any input on this...
>
> I've written a program that analyzes a bunch of data and stores some results in a two field structure array. Using the progressbar function (downloaded from file exchange), it was obvious that the beginning iterations of the loop complete much faster than later iterations, which told me that I needed to preallocate a chunk of memory for the structure. I'm pretty sure that I've now preallocated memory correctly, however while the program now runs faster than it did before, the estimated time remaining as the loop progresses continues to increase.
>
> As I mentioned, it's a two field structure. The size of the final structure will be somewhere in the (1,4000) to (1,6000) range. Each element in the structure contains a vector of variable length, say somewhere between (1,100) and (1,1101). 1101 is the maximum possible length of an individual element.
>
> I tried to preallocate space as follows:
>
> example_num = 4000;
>
> for i = 1:example_num
> master(i).mz = zeros(1101,1);
> master(i).int = zeros(1101,1);
> end
>
> With each iteration of the major loop in the program, each element of this structure is replaced by it's real vector, using something similar to:
>
> for j = 1:example_num
> %insert a bunch of other code here
> master(j).mz = random_vector1;
> master(j).int = random_vector2;
> end
>
> Does this seem like the space is preallocated correctly?
No the way you preallocate is completely off.
1) You need to preallocate the STRUCTURE array, not its field.
2) There is no gain to preallocate the fields with ZEROS then erase them later on with RANDOM_VECTOR*.
3) However you could preallocate the field only if you know ahead of time its size. In that case use the command: master(j).mz(:) = random_vector1;
That will not totally prevent your program to slowdown, since the RAM is occupied more when the loop go on.
Bruno
