Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting elements from a cell in Matlab

Tags:

matlab

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

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

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

I tried:

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

but I got the error message:

??? Error using ==> eq
Too many input arguments.

Error in ==> ind2remove = (A{1,:} == 0);
like image 528
bzak Avatar asked Jan 01 '12 18:01

bzak


People also ask

How do you remove an element from a matrix in MATLAB?

The easiest way to remove a row or column from a matrix is to set that row or column equal to a pair of empty square brackets [] . For example, create a 4-by-4 matrix and remove the second row.

Can you delete in MATLAB?

To remove commands you have run from your command history, click on the command window prompt and hit the up arrow. Then scroll up to the command you no longer want to show up and right click on it. Select "delete" and it will no longer appear in your command history.

What does %% mean in MATLAB?

Description: The percent sign is most commonly used to indicate nonexecutable text within the body of a program. This text is normally used to include comments in your code. Some functions also interpret the percent sign as a conversion specifier.


1 Answers

Indexing using { } gives you the contents of the cell, whereas indexing using ( ) returns the same type as the original object i.e., if A is a cell, A{i,j} will return what it's holding, and A(i,j) will always return a cell. You need the latter.

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

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

The assumption here is that each cell in the first row holds only a single numeric element, as per your comment.

like image 66
abcd Avatar answered Sep 28 '22 09:09

abcd