Borrar filtros
Borrar filtros

Checking rows of array for one of a set of values

1 visualización (últimos 30 días)
Adam Fitchett
Adam Fitchett el 3 de Mzo. de 2022
Respondida: Voss el 3 de Mzo. de 2022
How can I concisely check if each row of a matrix contains one or more of a certain set of values? I want to do something like this:
myMatrix = [0,0,0;0,0,1;0,0,0;0,0,2;0,0,0;0,0,1;0,0,0;0,0,2]
checkFor = [1,2,3]
any(myMatrix==checkFor,2)
ans = [2;4;6;8]
myMatrix = 8×3
0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 0 0 2
checkFor = 1×3
1 2 3
ans = 8×1 logical array
0 0 0 0 0 0 0 0
ans = 4×1
2 4 6 8

Respuesta aceptada

Voss
Voss el 3 de Mzo. de 2022
Use ismember():
myMatrix = [0,0,0;0,0,1;0,0,0;0,0,2;0,0,0;0,0,1;0,0,0;0,0,2];
checkFor = [1,2,3];
ism = any(ismember(myMatrix,checkFor),2);
find(ism)
ans = 4×1
2 4 6 8
ans = [2;4;6;8]
ans = 4×1
2 4 6 8

Más respuestas (0)

Categorías

Más información sobre Particle & Nuclear Physics en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by