Removing items from a cell in Matlab

In the matrix, to remove columns in which the element of the first row is 0, we can use:

ind2remove = (A(1,:) == 0); A(:,ind2remove) = []; 

How to do this if A is a cell? I want to remove columns in which the element of the first row is 0.

I tried:

 ind2remove = (A{1,:} == 0); A{:,ind2remove} = []; 

but I got an error message:

 ??? Error using ==> eq Too many input arguments. Error in ==> ind2remove = (A{1,:} == 0); 
+8
matlab
source share
1 answer

Indexing with { } gives you the contents of the cell, while indexing with ( ) returns the same type as the original object, i.e. if A is a cell, A{i,j} will return what it holds, and A(i,j) will always return the cell. You need the last.

So, in your case, you can do the following to exclude all columns where the first row has 0 .

 A(:, cellfun(@(x)x==0, A(1,:))) = []; 

It is assumed that each cell in the first row contains only one numeric element according to your comment.

+10
source share

All Articles