How to remove null entries inside an array of cells in MATLAB?

I have an array of cells in MATLAB, say cell_arr , and it has null entries as well as entries in non-zeros cells. For example:

 cell_arr = {0, 0, 0, 0, 0, {1x3 cell}, {1x3 cell}, {1x3 cell}, {1x3 cell}}; 

Can someone tell me how to remove these null entries from cell_arr or to find indices of non-null entries? In addition, I want to avoid the for loop to complete this task.

I have already tried the find function, however the find function is not applicable for cell arrays. I am wondering if there is one line expression / expression doing this job?

+7
matlab cell-array
source share
2 answers

As far as I know, there is no single line function. You need to combine some features. The first line finds zeros in your array of cells, and the second line deletes these entries. Note the brackets () iso {} to remove.

Try the following:

 idxZeros = cellfun(@(c)(isequal(c,0)), cell_arr); cell_arr(idxZeros) = []; 
+8
source share
 cell_arr(cellfun(@(x) ~x(1),cell_arr(:,1)),:) = [] 

Please let me know if this works.

0
source share

All Articles