Find complete rows that meet a condition

90 ビュー (過去 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
  1 件のコメント
Piet Bouwens
Piet Bouwens 2022 年 8 月 6 日
Ah that makes sense, thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by