I have a cell array in MATLAB, lets say cell_arr
and it has zero entries as well as non-zeros cell entries. For example:
cell_arr = {0, 0, 0, 0, 0, {1x3 cell}, {1x3 cell}, {1x3 cell}, {1x3 cell}};
Can somebody please tell how to remove these zero entries from the cell_arr
or, to find the indices of non-zero entries? Additionally, I want to avoid for
loop for performing this job.
I already tried find
function, however, find
function is not applicable for cell arrays. I am wondering if there exists a single line statement/expression doing this job?
As far as I know there is no single line function. You have to combine some functionallity.
The first line finds the zeros in your cell array, while the second line deletes those entries. Note the ()
parentheses i.s.o. {}
for removal.
Try this:
idxZeros = cellfun(@(c)(isequal(c,0)), cell_arr);
cell_arr(idxZeros) = [];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With