how can I check to see if two different matricies contain any of the same numbers?

For example I want to say A=[1 2 3 4 5 6] B=[7 1 8] If any element in matrix A is equal to matrix B then.... I want the statement to be true since 1 is contained in both matrix A and matrix B

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 18일
편집: Azzi Abdelmalek 2013년 1월 18일
any(ismember(B,A))

댓글 수: 3

I don't see how this would work. First, I don't know how you would ever get isequal to return anything other than a logical scalar, so it is not clear what the any is doing. Second, isequal(B,A) will compare B and A to see if they are identical (ignoring class, but including size). Finally, for the OP's example your answer returns false, but the correct answer is true.
Sorry, my connection is actually too slow, it took too long to edit my answer. I meant "ismember")
Well that makes a lot more sense. I think I like ismember better than intersect

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

What you are interested in is if A and B intersect. The intersect function will tell you which elements are in both A and B or return empty if there are no common elements. You can test if an array is empty with isempty. Since you want to return true if the intersection is not empty (i.e., there is overlap), you need to negate the answer. You can do this with not. Putting it all together gives:
not(isempty(intersect(A, B)))

카테고리

도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by