Find complete rows that meet a condition

103 vues (au cours des 30 derniers jours)
Piet Bouwens
Piet Bouwens le 6 Août 2022
Commenté : Piet Bouwens le 6 Août 2022
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.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 6 Août 2022
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 commentaire
Piet Bouwens
Piet Bouwens le 6 Août 2022
Ah that makes sense, thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by