Checking rows of array for one of a set of values

1 view (last 30 days)
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

Accepted Answer

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

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!