How to delete all rows of an array that have a specified number in a particular column?

1 次查看(过去 30 天)
How can you delete a row of an array if the number in a particular column doesn't equal a specified number or even multiple numbers? For example, let A = magic(5). How can we delete any row where A(2,:) = 5? B=A(A(:,2)~=5) doesn't quite do it, only gives me the first column. In this case, there's only one row, but I'm looking for a general solution.

采纳的回答

Walter Roberson
Walter Roberson 2022-3-30
A(:, A(2,:) == 5) = [];
  2 个评论
L'O.G.
L'O.G. 2022-3-30
编辑:L'O.G. 2022-3-30
@Walter Roberson Thanks, but that removes the column with that value, not the row. I realized (thanks to you!) that this is what I want: A(A(2,:) == 5,:) = [];
Walter Roberson
Walter Roberson 2022-3-30
How can we delete any row where A(2,:) = 5
A(2,:) is a query about contents of row 2, not about a particular column. It does not make sense to ask about removing rows for which something is true about row 2.
If you want to deal with columns, then
A = magic(5)
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
A(A(:,2) == 5, :) = []
A = 4×5
17 24 1 8 15 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by