I have a 1x6 cell array. In cells 1-4 and 6 I have a 5000x1 cell array that contains strings. In the fifth cell I have a 5000x1 cell array that contains numbers. Well, I want to remove the rows for which I have 0s in the fifth cell. To make sure, it is clear:
string string string string 35 string
string string string string 0 string
string string string string 42 string
string string string string 10 string
string string string string 0 string
I have found a similar question in the forum for a 1x2 cell, but I cannot figure out how to do it in this specific case.
Can anyone help me?
Thanks in advance.
Assume that your cell array is stored in the variable c
. First, we can determine which of the rows of element 5 are not equal to 0
using a logical operation:
notZeros = (c{5} ~= 0);
Then, loop through the elements and apply the logical index to filter out the zero rows:
for k = 1:6
c{k} = c{k}(notZeros);
end
You can also use cellfun
. It maybe slower but may also be more readable:
c = cellfun(@(c_el) c_el(notZeros),c,'UniformOutput',false);
"'UniformOutput',false
" is a required option since the filter outputs vectors which, as understood by the function, is a nonuniform result.