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

不问归期 提交于 2019-12-04 03:13:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!