Fast method of Initialization of logicals into cells

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jul 2011
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 Comments
Walter Roberson
Walter Roberson on 20 Jul 2011
Corrected in my code, thanks. Note thought that that gives an actual 1x1 logical value, not an empty logical.

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!