Got Questions? Get Answers.
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:
Preallocated a matrix to avoid that grows inside of a loop

Subject: Preallocated a matrix to avoid that grows inside of a loop

From: Jose

Date: 11 Dec, 2008 15:26:03

Message: 1 of 3

Hi everyone,
I am interested to optimize the next code where xc is not preallocated:

xc=[]
data = [ 1 2 3 4 5 6]
for i=1:4
 x=data(i:i+2)
 xc=[xc;x]
end

xc =

     1 2 3
     2 3 4
     3 4 5
     4 5 6

Anyone can tell me how is possible preallocated the matrix xc to obtain
a code more efficient?

Thanks in advance,
Jose.

Subject: Preallocated a matrix to avoid that grows inside of a loop

From: John D'Errico

Date: 11 Dec, 2008 16:03:18

Message: 2 of 3

"Jose " <jose.l.vega@gmail.com> wrote in message <ghrbea$bsr$1@fred.mathworks.com>...
> Hi everyone,
> I am interested to optimize the next code where xc is not preallocated:
>
> xc=[]
> data = [ 1 2 3 4 5 6]
> for i=1:4
> x=data(i:i+2)
> xc=[xc;x]
> end
>
> xc =
>
> 1 2 3
> 2 3 4
> 3 4 5
> 4 5 6
>
> Anyone can tell me how is possible preallocated the matrix xc to obtain
> a code more efficient?
>
> Thanks in advance,
> Jose.

As I just now responded to your last post, do not
grow the array at all. Instead preallocate it to be
the correct size. Here that means

xc = zeros(4,3);

Then use a matrix index to assign the values as
you create them.

xc(i,:) = x;

There are ways to efficiently grow an array, but
they are still not as good as preallocation.

http://www.mathworks.com/matlabcentral/fileexchange/8334

John

Subject: Preallocated a matrix to avoid that grows inside of a loop

From: Jose

Date: 11 Dec, 2008 17:37:01

Message: 3 of 3




"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ghrdk6$h86$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghrbea$bsr$1@fred.mathworks.com>...
> > Hi everyone,
> > I am interested to optimize the next code where xc is not preallocated:
> >
> > xc=[]
> > data = [ 1 2 3 4 5 6]
> > for i=1:4
> > x=data(i:i+2)
> > xc=[xc;x]
> > end
> >
> > xc =
> >
> > 1 2 3
> > 2 3 4
> > 3 4 5
> > 4 5 6
> >
> > Anyone can tell me how is possible preallocated the matrix xc to obtain
> > a code more efficient?
> >
> > Thanks in advance,
> > Jose.
>
> As I just now responded to your last post, do not
> grow the array at all. Instead preallocate it to be
> the correct size. Here that means
>
> xc = zeros(4,3);
>
> Then use a matrix index to assign the values as
> you create them.
>
> xc(i,:) = x;
>
> There are ways to efficiently grow an array, but
> they are still not as good as preallocation.
>
> http://www.mathworks.com/matlabcentral/fileexchange/8334
>
> John
>
Thank you John for helping me to optimize my code, I got it.
Cheers,
Jose.

Tags for 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