Effacer les filtres
Effacer les filtres

Checking rows of array for one of a set of values

1 vue (au cours des 30 derniers jours)
Adam Fitchett
Adam Fitchett le 3 Mar 2022
Réponse apportée : Voss le 3 Mar 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

Réponse acceptée

Voss
Voss le 3 Mar 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

Plus de réponses (0)

Catégories

En savoir plus sur Particle & Nuclear Physics dans Help Center et File Exchange

Tags

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by