How to return a row from a 2d matrix

If i have a matrix like this A =
52 53 17 10 8
16 12 7 14 23
24 90 1 19 36
4 3 21 11 18
and
B = 30 52 7 10 80
5 20 22 7 43
4 2 1 9 6
and i want to get the intersection between them and whenever there is an
intersection, i want it to print the row that contains the intersected element
in A.
Like here for example, 52 is in both matrices, so one of the outputs should be:
52 53 17 10 8
My code:
A= [52 53 17 10 8; 16 12 7 14 23; 24 90 1 19 36; 4 3 21 11 18]
B= [30 52 7 10 80;5 20 22 7 43; 4 2 1 9 6]
r = intersect(A,B); %The values of intersection between the two matrices
for i = 1 : length(r)
m = find(A == r(i)); %This gets the indices where they intersect
end

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 8 Mär. 2016
Bearbeitet: Azzi Abdelmalek am 8 Mär. 2016

0 Stimmen

A= [52 53 17 10 8; 16 12 7 14 23; 24 90 1 19 36; 4 3 21 11 18]
B= [30 52 7 10 80;5 20 22 7 43; 4 2 1 9 6]
idx=ismember(A,B)
C=A(idx)
indices=find(idx)

3 Kommentare

N.
N. am 8 Mär. 2016
My code already does so, but i need to return the whole row that contains the intersection number!
Azzi Abdelmalek
Azzi Abdelmalek am 8 Mär. 2016
Bearbeitet: Azzi Abdelmalek am 8 Mär. 2016
What is the expected result?
out=A(any(ismember(A,B),2),:)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by