Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Memory pre-allocation issue

Subject: Memory pre-allocation issue

From: Alex

Date: 7 Aug, 2007 14:46:38

Message: 1 of 9

Hi group:

I'm in the process of optimising code that someone else
wrote.

Can anyone tell me how to pre-allocate memory for a <1xd
cell> array.

So far I've failed miserably.

Cheers,

Alex

Subject: Memory pre-allocation issue

From: Penny Anderson

Date: 7 Aug, 2007 14:54:56

Message: 2 of 9

"Alex " <aroussel@firthrixson.com> wrote in message
news:f9a0ke$7ar$1@fred.mathworks.com...
> Hi group:
>
> I'm in the process of optimising code that someone else
> wrote.
>
> Can anyone tell me how to pre-allocate memory for a <1xd
> cell> array.
>
> So far I've failed miserably.
>
> Cheers,
>
> Alex

C = cell(1,d) will look like it, but there isn't anything really there.

Penny.


Subject: Memory pre-allocation issue

From: David

Date: 7 Aug, 2007 14:59:15

Message: 3 of 9

"Alex " <aroussel@firthrixson.com> wrote in message
<f9a0ke$7ar$1@fred.mathworks.com>...
> Hi group:
>
> I'm in the process of optimising code that someone else
> wrote.
>
> Can anyone tell me how to pre-allocate memory for a <1xd
> cell> array.
>
> So far I've failed miserably.
>
> Cheers,
>
> Alex

since a 'cell' can contain just about anything, how could
you preallocate memory for it? it makes sense to allocate
an array of numbers because you know how big they are and
how they are accessed. but a cell array could have
elements of different sizes with no way to know in advance.

Subject: Memory pre-allocation issue

From: Alex

Date: 7 Aug, 2007 15:08:23

Message: 4 of 9

"David " <dave@bigcompany.com> wrote in message <f9a1c2$mv8
$1@fred.mathworks.com>...
> "Alex " <aroussel@firthrixson.com> wrote in message
> <f9a0ke$7ar$1@fred.mathworks.com>...
> > Hi group:
> >
> > I'm in the process of optimising code that someone else
> > wrote.
> >
> > Can anyone tell me how to pre-allocate memory for a
<1xd
> > cell> array.
> >
> > So far I've failed miserably.
> >
> > Cheers,
> >
> > Alex
>
> since a 'cell' can contain just about anything, how could
> you preallocate memory for it? it makes sense to
allocate
> an array of numbers because you know how big they are and
> how they are accessed. but a cell array could have
> elements of different sizes with no way to know in
advance.

True, and I have to admit I hadn't thought about this.

So, taking the worst case scenario in my case, the <1xd
cell> array would contain <1xd double> vectors.
How can I preallocate that ?

NB: i'm onl;y familiar with 'a=zeros(1,d)'-type pre-
allocation routines.

Alex

Subject: Memory pre-allocation issue

From: Kelly Kearney

Date: 7 Aug, 2007 15:27:43

Message: 5 of 9

Alex wrote:

> So, taking the worst case scenario in my case, the <1xd
> cell> array would contain <1xd double> vectors.
> How can I preallocate that ?
>
> NB: i'm onl;y familiar with 'a=zeros(1,d)'-type pre-
> allocation routines.

If I know in advance that all the elements of a cell array
are going to hold equally-sized arrays, I usually use deal
to preallocate, e.g.

ncell = 5;
nelement = 10;
newcell = cell(ncell,1);
[newcell{:}] = deal(zeros(1,nelement))


newcell =

    [1x10 double]
    [1x10 double]
    [1x10 double]
    [1x10 double]
    [1x10 double]

-Kelly




Subject: Memory pre-allocation issue

From: Titus

Date: 7 Aug, 2007 15:30:33

Message: 6 of 9


"Alex " <aroussel@firthrixson.com> schrieb im Newsbeitrag
news:f9a1t7$5ft$1@fred.mathworks.com...
> "David " <dave@bigcompany.com> wrote in message <f9a1c2$mv8
> $1@fred.mathworks.com>...
>> "Alex " <aroussel@firthrixson.com> wrote in message
>> <f9a0ke$7ar$1@fred.mathworks.com>...
>> > Hi group:
>> >
>> > I'm in the process of optimising code that someone else
>> > wrote.
>> >
>> > Can anyone tell me how to pre-allocate memory for a
> <1xd
>> > cell> array.
>> >
>> > So far I've failed miserably.
>> >
>> > Cheers,
>> >
>> > Alex
>>
>> since a 'cell' can contain just about anything, how could
>> you preallocate memory for it? it makes sense to
> allocate
>> an array of numbers because you know how big they are and
>> how they are accessed. but a cell array could have
>> elements of different sizes with no way to know in
> advance.
>
> True, and I have to admit I hadn't thought about this.
>
> So, taking the worst case scenario in my case, the <1xd
> cell> array would contain <1xd double> vectors.
> How can I preallocate that ?
>
> NB: i'm onl;y familiar with 'a=zeros(1,d)'-type pre-
> allocation routines.
>
> Alex

Hi Alex,

one way could be:

x = cell(1,d);
[x{:}] = deal(zeros(1,d))

but in this case: why not switch do a dxd matrix instead?

Titus


Subject: Memory pre-allocation issue

From: Loren Shure

Date: 7 Aug, 2007 15:47:06

Message: 7 of 9

In article <f9a36q$4s8$1@fred.mathworks.com>,
titus.edelhofer@mathworks.de says...
>
> "Alex " <aroussel@firthrixson.com> schrieb im Newsbeitrag
> news:f9a1t7$5ft$1@fred.mathworks.com...
> > "David " <dave@bigcompany.com> wrote in message <f9a1c2$mv8
> > $1@fred.mathworks.com>...
> >> "Alex " <aroussel@firthrixson.com> wrote in message
> >> <f9a0ke$7ar$1@fred.mathworks.com>...
> >> > Hi group:
> >> >
> >> > I'm in the process of optimising code that someone else
> >> > wrote.
> >> >
> >> > Can anyone tell me how to pre-allocate memory for a
> > <1xd
> >> > cell> array.
> >> >
> >> > So far I've failed miserably.
> >> >
> >> > Cheers,
> >> >
> >> > Alex
> >>
> >> since a 'cell' can contain just about anything, how could
> >> you preallocate memory for it? it makes sense to
> > allocate
> >> an array of numbers because you know how big they are and
> >> how they are accessed. but a cell array could have
> >> elements of different sizes with no way to know in
> > advance.
> >
> > True, and I have to admit I hadn't thought about this.
> >
> > So, taking the worst case scenario in my case, the <1xd
> > cell> array would contain <1xd double> vectors.
> > How can I preallocate that ?
> >
> > NB: i'm onl;y familiar with 'a=zeros(1,d)'-type pre-
> > allocation routines.
> >
> > Alex
>
> Hi Alex,
>
> one way could be:
>
> x = cell(1,d);
> [x{:}] = deal(zeros(1,d))
>
> but in this case: why not switch do a dxd matrix instead?
>
> Titus
>
>
>

Actually that won't full initialize everything because MATLAB will be
smart and have all the zero arrays point to the same array, until they
each start getting filled with something different.

--
Loren
http://blogs.mathworks.com/loren/

Subject: Memory pre-allocation issue

From: Darik

Date: 7 Aug, 2007 21:18:12

Message: 8 of 9

Loren Shure <loren@mathworks.com> wrote in message
>
> Actually that won't full initialize everything because
MATLAB will be
> smart and have all the zero arrays point to the same
array, until they
> each start getting filled with something different.

This should work, right?

%Preallocate a cell array containing vectors of random lengths
numVectors = 10;
vectorLengths = round(9*rand(1,numVectors))+1;
ncell = arrayfun(@(x) (zeros(1,x)), vectorLengths, 'uni',
false);

Subject: Memory pre-allocation issue

From: us

Date: 7 Aug, 2007 23:11:16

Message: 9 of 9

Alex:
<SNIP initializing an array of ML constructs...

this has been discussed extensively here at

http://www.mathworks.com/matlabcentral/newsreader/view_threa
d/146789

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
preallocate Alex 7 Aug, 2007 10:50:18
memory Alex 7 Aug, 2007 10:50:18
cell array Alex 7 Aug, 2007 10:50:18
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics