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
0 个评论
采纳的回答
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
2011-7-20
Corrected in my code, thanks. Note thought that that gives an actual 1x1 logical value, not an empty logical.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!