Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove zero entries inside a cell array in MATLAB?

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?

like image 301
Sanchit Avatar asked Sep 11 '13 12:09

Sanchit


1 Answers

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) = [];
like image 67
Nick Avatar answered Sep 29 '22 09:09

Nick