Fast method of Initialization of logicals into cells

1 次查看(过去 30 天)
Hi....i'd like to make a large cell array (that contains empty logicals) and i want to see if theres anyway to do it faster than the way im currently doing it....
my way is described below...
temp_allzeros = false(size(config.MIDlist,1),1);
allzeros = cell(size(config.MIDlist,1),1);
for i = 1:length(temp_allzeros)
allzeros(i) = temp_allzeros(i);
end

采纳的回答

Walter Roberson
Walter Roberson 2011-7-20
You cannot set a cell array entry to a logical value.
>> bar = cell(3,1)
bar =
[]
[]
[]
>> bar(1) = false
??? Conversion to cell from logical is not possible.
In your code, there is no point using temp_allzeros as an array since every member is the same. Your code could be replaced with
allzeros = cell(size(config.MIDlist,1),1);
for i = 1:length(temp_allzeros)
allzeros{i} = false;
end
But faster would be
allzeros(1:size(config.MIDlist,1)) = {false}; %corrected per Sean
Note, though, that in your original code and in my code, the cell entries are set to actual logicals, not the empty logical. So you need to jigger the code more:
allzeros(1:size(config.MIDlist,1)) = {false(0,0)};
As you asked about "faster", you might want to do timing tests to compare
[allzeros{1:size(config.MIDlist,1)}] = deal(false(0,0));
  2 个评论
Walter Roberson
Walter Roberson 2011-7-20
Corrected in my code, thanks. Note thought that that gives an actual 1x1 logical value, not an empty logical.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by