问题
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?
回答1:
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) = [];
回答2:
cell_arr(cellfun(@(x) ~x(1),cell_arr(:,1)),:) = []
Please let me know if this works.
来源:https://stackoverflow.com/questions/18740802/how-to-remove-zero-entries-inside-a-cell-array-in-matlab