"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.
"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.
"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.
> 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.
"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?
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 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);
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.