Find complete rows that meet a condition

103 visualizaciones (últimos 30 días)
Piet Bouwens
Piet Bouwens el 6 de Ag. de 2022
Comentada: Piet Bouwens el 6 de Ag. de 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.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 6 de Ag. de 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

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by