From: C T on 9 Apr 2010 12:17 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
From: Bruno Luong on 9 Apr 2010 12:33 "C T" <usro(a)doramail.com> wrote in message <hpnjul$3vs$1(a)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
From: James Tursa on 9 Apr 2010 12:44 "C T" <usro(a)doramail.com> wrote in message <hpnjul$3vs$1(a)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
From: David Young on 9 Apr 2010 12:56 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');
From: Jan Simon on 9 Apr 2010 15:25 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
|
Next
|
Last
Pages: 1 2 Prev: preallocating a cell array of strings Next: Saving Matlab Plots for Webpages |