|
|
preallocating a cell array of strings
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
|
|
0
|
|
|
|
Reply
|
C
|
4/9/2010 4:17:26 PM |
|
"C T" <usro@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
|
|
0
|
|
|
|
Reply
|
Bruno
|
4/9/2010 4:33:24 PM
|
|
"C T" <usro@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);
>
> But I thought there may be a better solution somewhere. The main thing is being able to do unique(test);
>
> TIA
> ct
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
|
|
0
|
|
|
|
Reply
|
James
|
4/9/2010 4:44:22 PM
|
|
|
2 Replies
236 Views
(page loaded in 0.148 seconds)
Similiar Articles: syntax for creating array of cell arrays - comp.soft-sys.matlab ...ATTENTION: Preallocating within struct (and cell) might not needed (thus a waste), depending ... gettting index corresponding to string in cell array of strings ... Create ... Convert all cell array to string - comp.soft-sys.matlabHi i have a cell array with some strings and some numbers all mixed up. How can i convert each of my cells to a string? Cheers ... Concat string to cell array of strings - comp.soft-sys.matlab ...Hi, I'm trying to concat a string to each string in a cell array of strings using: first = 'first_' attach = { 'end' 'end1' 'end31' } i=1:1:si... Find a string in a cell array - comp.soft-sys.matlabFind a string in a cell array - comp.soft-sys.matlab Find a string in a cell array - comp.soft-sys.matlab Find a string in a cell array - comp.soft-sys.matlab Find ... gettting index corresponding to string in cell array of strings ...Sounds simple (and might be simple too): Suppose you have a cell array of strings, like cellArray = {'I' 'want' 'this' 'string'} and now want the ... Converting a cell array into a dataset - comp.soft-sys.matlab ...I have two cell arrays obtained from a database fetch: data (R x C) - contains data with columns of doubles and strings data_fields (C x 1) - contai... Sorting Strings and uniquifying - comp.soft-sys.matlabI have a bunch of image, for each image I want to associate a subject string. Forming a cell array or an array of strings -- not a problem. Then I... Remove special characters from contents of a cell array - comp ...I have a cell array which contains string elements, but embedded in those strings are special characters (like carriage returns) that are messing up s... converting from dataset (statistics toolbox) to cell array - comp ...I have a varibale A, of dataset class (250 by 1), the contents are all string. I need to convert that into a cell array of (250 by 1), unfortunately t... cell array to csv - comp.soft-sys.matlabConvert all cell array to string - comp.soft-sys.matlab cell array to csv - comp.soft-sys.matlab If your cell array is composed of all strings you could use ... to csv ... preallocating a cell array of strings - Newsreader - MATLAB CentralFile exchange, MATLAB Answers, newsgroup access, Links, and Blogs for the MATLAB & Simulink user community Techniques for Improving Performance :: Performance (MATLABĀ®)Preallocating Arrays. for and while loops that incrementally increase, or ... Numeric. zeros. y = zeros(1, 100); Cell. cell. B = cell(2, 3); B{1,3} = 1:3; B{2,2} = 'string'; 7/26/2012 3:40:34 PM
|
|
|
|
|
|
|
|
|