Find complete rows that meet a condition

조회 수: 102 (최근 30일)
Piet Bouwens
Piet Bouwens 2022년 8월 6일
댓글: Piet Bouwens 2022년 8월 6일
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