Is there a way to check each index of a matrix against each index of another smaller matrix?

조회 수: 1 (최근 30일)
Is there a way to check each index of a matrix against each index of another smaller matrix? I am trying to create a code that will check the index of one matrix, A, and compare it to another matrix, B. I want to find a row vector in A where none of the index in that row are equal to any of the index in B. I was thinking of using a for loop and somehow incorporating the find() function, but I am not sure where to start. Any tips would be appreciated.

채택된 답변

Voss
Voss 2022년 4월 20일
% a matrix:
A = magic(5)
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
% a smaller matrix:
B = [1 15 6; 20 12 21; 25 2 9]
B = 3×3
1 15 6 20 12 21 25 2 9
% index(es) of row(s) of A that have no elements in B:
find(~any(ismember(A,B),2))
ans = 2
% breaking that expression down a little bit:
ismember(A,B)
ans = 5×5 logical array
0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 1 1
any(ismember(A,B),2)
ans = 5×1 logical array
1 0 1 1 1
~any(ismember(A,B),2)
ans = 5×1 logical array
0 1 0 0 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by