Azzera filtri
Azzera filtri

Checking rows of array for one of a set of values

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Voss
Voss il 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

Più risposte (0)

Categorie

Scopri di più su Particle & Nuclear Physics in Help Center e File Exchange

Tag

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by