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);
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With