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 when you don't know the size of a matrix

Subject: Preallocate when you don't know the size of a matrix

From: Javier

Date: 20 May, 2011 16:00:21

Message: 1 of 5

Hi, I'm going to fill a matrix but I don't have any idea of its final size. Is it efficient to preallocate with zeros (X,X) being X a large number and then delete the zeros that stay? Thank you!

Subject: Preallocate when you don't know the size of a matrix

From: Andy

Date: 20 May, 2011 16:09:05

Message: 2 of 5

"Javier " <javsanzperez@gmail.com> wrote in message <ir636l$3pm$1@newscl01ah.mathworks.com>...
> Hi, I'm going to fill a matrix but I don't have any idea of its final size. Is it efficient to preallocate with zeros (X,X) being X a large number and then delete the zeros that stay? Thank you!

Could you give a little more detail, and possibly some sample code? Depending on the setup, a cell array might make sense. Or if you have R2011a, it might not matter (since they did some optimization in the non-preallocated case). But we need more info than what you've given us.

Subject: Preallocate when you don't know the size of a matrix

From: Javier

Date: 20 May, 2011 16:20:20

Message: 3 of 5

"Andy" wrote in message <ir63n1$55g$1@newscl01ah.mathworks.com>...
> "Javier " <javsanzperez@gmail.com> wrote in message <ir636l$3pm$1@newscl01ah.mathworks.com>...
> > Hi, I'm going to fill a matrix but I don't have any idea of its final size. Is it efficient to preallocate with zeros (X,X) being X a large number and then delete the zeros that stay? Thank you!
>
> Could you give a little more detail, and possibly some sample code? Depending on the setup, a cell array might make sense. Or if you have R2011a, it might not matter (since they did some optimization in the non-preallocated case). But we need more info than what you've given us.

Hi, thanks for answering. I want to detect elliptic objects and save the coordinates in a matrix. Depending on the images I'll have more or less. Maybe they go from 50 to 500.
Thank you

Subject: Preallocate when you don't know the size of a matrix

From: John D'Errico

Date: 20 May, 2011 16:33:04

Message: 4 of 5

"Javier " <javsanzperez@gmail.com> wrote in message <ir636l$3pm$1@newscl01ah.mathworks.com>...
> Hi, I'm going to fill a matrix but I don't have any idea of its final size. Is it efficient to preallocate with zeros (X,X) being X a large number and then delete the zeros that stay? Thank you!

This is one approach, that works very nicely if you have
a number that you expect won't be exceeded.

A more general solution is to use my growdata tools,
posted on the FEX. They use a combination strategy that
is robust to any final size of the array.

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

John

Subject: Preallocate when you don't know the size of a matrix

From: Javier

Date: 23 May, 2011 10:43:02

Message: 5 of 5

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ir6540$9bs$1@newscl01ah.mathworks.com>...
> "Javier " <javsanzperez@gmail.com> wrote in message <ir636l$3pm$1@newscl01ah.mathworks.com>...
> > Hi, I'm going to fill a matrix but I don't have any idea of its final size. Is it efficient to preallocate with zeros (X,X) being X a large number and then delete the zeros that stay? Thank you!
>
> This is one approach, that works very nicely if you have
> a number that you expect won't be exceeded.
>
> A more general solution is to use my growdata tools,
> posted on the FEX. They use a combination strategy that
> is robust to any final size of the array.
>
> http://www.mathworks.com/matlabcentral/fileexchange/8334
>
> John
Thanks, growdata2 works perfect!

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