"Think two, count blue." <roberson@hushmail.com> wrote in message <il3nf0$4cc$1@nrcnews.nrc.ca>...
> On 110307 04:18 PM, Jane wrote:
>
> > I cannot think of a way to preallocate my matrix. I have a nested for loop
> > with a calculation in the inner loop. The output is set a of 30x3 array
> > multiplied by the outer loop index. I am using Vertcat to concactinate this
> > into one array in the inner loop. Matlab complains that this array is growing
> > in size/changing and i should preallocate for speed. The final size is known.
> > How am I able to store this data without using vertcat for preallocation
> > purposes.
> >
> > for example,
> >
> > for aa = 1:90
> > for a = 1:50
> > p=calculation
> > if a==1 && aa==1
> > pp=p;
> > else
> > pp=vertcat(p,pp)
> > end end
> > end
> >
> > Thank you
>
> I don't see where you get the 30 x 3 when "a" goes to 50 ?
>
> Assuming that you meant 50 x 3,
>
> pp = zeros(50*90,3);
> off = size(pp,1);
>
> for aa = 1:90
> for a = 1:50
> pp(off, :) = calculation;
> off = off  1;
> end
> end
>
> Here I retained the behaviour you had of building the array _backwards_ .
>
> There are some approaches that can be more efficient internally, such as
> switching around to use a 3 x N array during the time you build it,
> transposing it at the end if you need to. Depending on the calculation you are
> doing, it might be possible to do all of the "a" values at once.
Hi,
everytime the inner loop goes around I get an array of 30x3 . the result is that and then it is placed in pp. not only that. The first time the inner loop goes around i need to store 1:30 values then remaining loop i need to store 2:30 values skipping row 1, which I havent figured out yet. And then once all 50 inner loop is complete the outer loop would increment, meaning that there will be another set of [30,3] multiplied by 50
