how can I check to see if two different matricies contain any of the same numbers?
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Azzi Abdelmalek
2013 年 1 月 18 日
編集済み: Azzi Abdelmalek
2013 年 1 月 18 日
any(ismember(B,A))
3 件のコメント
Azzi Abdelmalek
2013 年 1 月 18 日
Sorry, my connection is actually too slow, it took too long to edit my answer. I meant "ismember")
Daniel Shub
2013 年 1 月 18 日
Well that makes a lot more sense. I think I like ismember better than intersect
その他の回答 (1 件)
Daniel Shub
2013 年 1 月 18 日
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)))
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!