How to compare an array with 2D matrix?

If I have an array A= [1,2] and matrix B= [3,4;8,6;1,2], How can I say that array A is present in B?

 採用された回答

KSSV
KSSV 2016 年 10 月 21 日

1 投票

doc ismember

1 件のコメント

Emmanuel
Emmanuel 2016 年 10 月 21 日
Thanks a lot! It works!! You are very helpful

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 10 月 21 日

2 投票

Try ismember() with the 'rows' option:
[~, matchingRows] = ismember(A, B, 'rows')

3 件のコメント

Emmanuel
Emmanuel 2016 年 10 月 21 日
Thanks!! That was really helpul. Is there a way I could return the indices? Also, it doesn't work when I have same elements multiple time in my MAtrix B. A returns only the first match in B, whereas the other matches are not getting noticed.
Image Analyst
Image Analyst 2016 年 10 月 21 日
Emmanuel, try this:
A= [1,2]
B= [3,4;8,6;1,2; 4,5; 1,2]
[inA, inB] = ismember(B, A, 'rows')
matchingRows = find(inB)
Emmanuel
Emmanuel 2016 年 10 月 21 日
Yeah! That works. Thank you very much..

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2016 年 10 月 21 日

コメント済み:

2016 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by