Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

preallocating a cell array of strings

415 views
Skip to first unread message

C T

unread,
Apr 9, 2010, 12:15:24 PM4/9/10
to
I am trying to pre-allocate a cell array of strings. I can do the following:

test = cell(m,1);
for i = 1:m
test{i} = 'test';
end;
unique(test);

But I thought there may be a better solution somewhere. The main thing is being able to do unique(test);

TIA
ct

C T

unread,
Apr 9, 2010, 12:17:26 PM4/9/10
to

Bruno Luong

unread,
Apr 9, 2010, 12:33:24 PM4/9/10
to
"C T" <us...@doramail.com> wrote in message <hpnjul$3vs$1...@fred.mathworks.com>...

> I am trying to pre-allocate a cell array of strings. I can do the following:
>
> test = cell(m,1);
> for i = 1:m
> test{i} = 'test';
> end;
> unique(test);

Generally there is no gain to preallocate elements of cell if you intend to replace them later (this situation likely happens when using cell). See this thread for more detailed discussion:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/258931

Bruno

James Tursa

unread,
Apr 9, 2010, 12:44:22 PM4/9/10
to
"C T" <us...@doramail.com> wrote in message <hpnjul$3vs$1...@fred.mathworks.com>...

test = cell(m,1);
test(:) = {'test'};

This will cause each cell to share the same physical variable 'test', so not much wasted space even if m is very large. Also gets rid of the [] cells so you can use unique.

James Tursa

David Young

unread,
Apr 9, 2010, 12:56:23 PM4/9/10
to
It may depend on your criteria for "better", and also I'm not sure what you mean by saying that the main thing is the call to unique, but here is one way to set up the cell array without a loop, and with the string 'test' in every cell:

[test{1:m, 1}] = deal('test');

Jan Simon

unread,
Apr 9, 2010, 3:25:07 PM4/9/10
to
Dear C T!

This is much faster than the DEAL method:
test = cell(m, 1);
test(:) = {'test'};

Jan

Loren Shure

unread,
Apr 12, 2010, 10:33:45 AM4/12/10
to
In article <hpnuuj$q7q$1...@fred.mathworks.com>,
matlab.T...@nMINUSsimon.de says...

I haven't looked, but I bet the test(:) = {'test'} uses a shared data
copy for the string value, whereas the deal version might not. So part
of the difference might be the extra real memory allocation going on.
Just a guess though.

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

us

unread,
Apr 12, 2010, 10:46:04 AM4/12/10
to
Loren Shure <loren...@mathworks.com> wrote in message <MPG.262cf9567...@news.mathworks.com>...

> In article <hpnuuj$q7q$1...@fred.mathworks.com>,
> matlab.T...@nMINUSsimon.de says...
> > Dear C T!
> >
> > > I am trying to pre-allocate a cell array of strings. I can do the following:
> > >
> > > test = cell(m,1);
> > > for i = 1:m
> > > test{i} = 'test';
> > > end;
> > > unique(test);
> > >
> > > But I thought there may be a better solution somewhere. The main thing is being able to do unique(test);
> >
> > This is much faster than the DEAL method:
> > test = cell(m, 1);
> > test(:) = {'test'};
> >
> > Jan
> >
>
> I haven't looked, but I bet the test(:) = {'test'} uses a shared data
> copy for the string value, whereas the deal version might not. So part
> of the difference might be the extra real memory allocation going on.
> Just a guess though.

loren
actually, DEAL is using a shared copy...
something along this line has been discussed several times in CSSM, eg,

http://www.mathworks.com/matlabcentral/newsreader/view_thread/146789#369303

urs

0 new messages