1
votes

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.

1
Sorry I don't understand your example can you make it clearer? What exactly is string ... 35 string ...? Is it the 5th cell from which you want to remove the 0's? - Benoit_11
Sorry, in the preview seemed that everything was properly sorted. I want to remove the rows that have a 0 in the fifth cell. The strings are codes that are needed for a correct interpretation of the data. Thus, if I have a 0 element, I do not need the codes either. Is this clearer? - aulky11

1 Answers

1
votes

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.