Find complete rows that meet a condition

103 次查看(过去 30 天)
I would like to find a way to select rows from an array that meet a certain condition. In the case of single elements this is very simple, but I don't know how to do it for full rows/columns. For example, if you have the array
A = [1 2;
3 1;
0 6;
2 0]
and the variable
x1 = [3 1]
I would want to know the rows in A that are not equal to x1, so A(1,:), A(3,:) and A(4,:). Of course it's easy to just make a for loop, but I wonder if there's a simpler/more elegent way.

采纳的回答

Cris LaPierre
Cris LaPierre 2022-8-6
You could use two conditions - one for the first column and another for the 2nd column - to find the rows that meet your criteria.
A = [1 2;
3 1;
0 6;
2 0];
x1 = [3 1];
% row numbers
rows = find(A(:,1)~=x1(1) & A(:,2)~=x1(2))
rows = 3×1
1 3 4
% row values
A(rows,:)
ans = 3×2
1 2 0 6 2 0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by