remove rows from a matrix on a specific condition

I want to remove rows from a matrix when a specific condition is satisfied.
For example, lets as assume that I have the following 5x5 matrix :
A= [ 5 3 3 1 4
6 6 3 1 0
6 2 2 2 0
4 2 3 2 0
1 1 1 1 2 ]
and I want to delete the rows of this matrix when the elements of the 5th column are equal to 0, so I will be left with the first and last row as an outcome:
A= [ 5 3 3 1 4
1 1 1 1 2 ]
Thanks

1 个评论

Just to make clear there are ; for every 5 elements in the example to separate the rows, I hope it is clear, sorry about that

请先登录,再进行评论。

 采纳的回答

Try
A= [ 5 3 3 1 4; 6 6 3 1 0; 6 2 2 2 0; 4 2 3 2 0; 1 1 1 1 2 ]
A(A(:, 5)== 0, :)= []

3 个评论

I have tried to do this for the table variable type, but I get the following error message:
Undefined operator '~=' for input arguments of type 'table'
It seems that logical indexing doesn't work in the same way for table variables. What would be an equivalent procedure?
What if the 0 is not exactly in the 5th Column but random in cells, then how to solve the same issue?
@Raghu Vamsi, If still needed, repeat for every column. As it wil only remove the rows with a 0 in that specific column.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by