Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Preallocate matrix

Subject: Preallocate matrix

From: Jane

Date: 7 Mar, 2011 22:18:20

Message: 1 of 4


Hi,

I cannot think of a way to pre-allocate 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

Subject: Preallocate matrix

From: Think two, count blue.

Date: 7 Mar, 2011 22:50:06

Message: 2 of 4

On 11-03-07 04:18 PM, Jane wrote:

> I cannot think of a way to pre-allocate 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.

Subject: Preallocate matrix

From: Jane

Date: 7 Mar, 2011 23:30:17

Message: 3 of 4

"Think two, count blue." <roberson@hushmail.com> wrote in message <il3nf0$4cc$1@nrc-news.nrc.ca>...
> On 11-03-07 04:18 PM, Jane wrote:
>
> > I cannot think of a way to pre-allocate 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

Subject: Preallocate matrix

From: Bruno Luong

Date: 8 Mar, 2011 07:55:07

Message: 4 of 4

"Jane" wrote in message <il3pq9$dnb$1@fred.mathworks.com>...
>
>
> 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

Very confuse.

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us