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?

 Akzeptierte Antwort

KSSV
KSSV am 21 Okt. 2016

1 Stimme

doc ismember

Weitere Antworten (1)

Image Analyst
Image Analyst am 21 Okt. 2016

2 Stimmen

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

3 Kommentare

Emmanuel
Emmanuel am 21 Okt. 2016
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.
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 am 21 Okt. 2016
Yeah! That works. Thank you very much..

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 21 Okt. 2016

Kommentiert:

am 21 Okt. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by