Deleting rows in a cell array based on a specific value in a column

Hi, I'm very new to Matlab and struggle with the following code. Any advice would be appreciated.
I have an cell array 7 x 2 cells. I would like to delete any rows when Column 2 contains 0.
A =
7×2 cell array
{[ 0]} {[0]}
{[ 1]} {[0]}
{[ 2]} {[0]}
{[ 3]} {[0]}
{[ 16]} {[1]}
{[ 17]} {[1]}
{[ 18]} {[1]}
Desirable outcome would be
A =
3×2 cell array
{[ 16]} {[1]}
{[ 17]} {[1]}
{[ 18]} {[1]}
I tried the following code but it gave me an error and I don't know how to resolve it. I know the issue is the probably the {} type but I don't know how to fix it.
A(A(:,2) == 0, :) = [];
Operator '==' is not supported for operands of type 'cell'.

 Respuesta aceptada

Do this:
A([A{:,2}] == 0, :) = [];
The [A{:,2}] part horizontally concatenates all the contents of the cells in the second column of A, producing a numeric array (that you can then compare with 0 and so on).

3 comentarios

Hi Benjamin, thank you so much for your help. Now I understand the change of { } could make it to numeric array.
I have a follow-up question. In the following cell arrary, I have an empty cell in the 1st column. Output read as below.
A =
8×2 cell array
{[ 0]} {[0]}
{[ 1]} {[0]}
{[ 2]} {[0]}
{[ 3]} {[0]}
{[ 16]} {[1]}
{[ 17]} {[1]}
{[ 18]} {[1]}
{0×0 double} {[0]}
{0x0 double} is just an [ ] if read it in the workspace. I would like to remove any rows with [ ] in the 1st column.
A([A{:,1}] == [], :) = [];
This would give me the error of "Error using == Arrays have incompatible sizes for this operation."
Ultimately, I would like to remove the any rows containing [ ] and 0 in the 1st column, and then remove the any rows containing 1 in the 2nd column.
Desirable output would be:
A =
3×2 cell array
{[ 1]} {[0]}
{[ 2]} {[0]}
{[ 3]} {[0]}
With your suggestion, I know how to do the 2nd part to remove any rows containing 1 in the 2nd column. But I can't figure out the 1st part. Your advice would be much appreicated!
Voss
Voss el 10 de Dic. de 2021
Editada: Voss el 10 de Dic. de 2021
For dealing with empty entries in a cell array, you cannot take the [A{:}] approach because the empty cells wouldn't show up. Instead, you can use cellfun. For instance:
cellfun(@isempty,A)
will return a logical array the same size of A with value true where the corresponding cell in A is empty and false where the corresponding cell in A is not empty.
So to remove rows of A where the cell in the first column is empty, you can say:
A(cellfun(@isempty,A(:,1)),:) = [];
or another way to say the same thing:
A(cellfun(@(x)isempty(x),A(:,1)),:) = [];
Or to do the whole thing (remove any row with [] or 0 in column 1, or any row with 1 in column 2):
A(cellfun(@(x)isempty(x) || x == 0, A(:,1).') | [A{:,2}] == 1,:) = [];
(where the transpose .' operation is done so that both inputs to the element-wise or operator | have the same size - recall that [A{:,2}] does horizontal concatenation and A(:,1) is a column, so we have to transpose one or the other; we also could've used vertical concatentation instead of horizontal via the vertcat function.)
This wouldn't work if there's an empty cell in the 2nd column, though, but at this point, I'll leave it to you to figure out why that is and how to get around that if necessary.
Thank you so much for such clear and elaborative answers!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2021a

Etiquetas

Preguntada:

SYL
el 9 de Dic. de 2021

Comentada:

SYL
el 10 de Dic. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by