Replace empty cells with logical 0's before cell2mat in MATLAB

Posted by Doresoom on Stack Overflow See other posts from Stack Overflow or by Doresoom
Published on 2010-04-12T17:29:30Z Indexed on 2010/04/12 17:33 UTC
Read the original article Hit count: 519

Filed under:
|
|
|
|

I've got a cell array of empty cells and ones that I want to convert to a logical array, where the empty cells are zeros. When I use cell2mat, the empty cells are ignored, and I end up with a matrix of solely 1's, with no reference to the previous index they held. Is there a way to perform this operation without using loops?

Example code:

for n=1:5              %generate sample cell array
    mycellarray{n}=1;
end
mycellarray{2}=[]      %remove one value for testing

Things I've tried:

mylogicalarray=logical(cell2mat(mycellarray));

which results in [1,1,1,1], not [1,0,1,1,1].

for n=1:length(mycellarray)
    if isempty(mycellarray{n})
       mycellarray{n}=0;
    end
end
mylogicalarray=logical(cell2mat(mycellarray));

which works, but uses loops.

© Stack Overflow or respective owner

Related posts about matlab

Related posts about cell