필터 지우기
필터 지우기

Checking rows of array for one of a set of values

조회 수: 1 (최근 30일)
Adam Fitchett
Adam Fitchett 2022년 3월 3일
답변: Voss 2022년 3월 3일
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

채택된 답변

Voss
Voss 2022년 3월 3일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by