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

 Accepted Answer

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 Comments

My code already does so, but i need to return the whole row that contains the intersection number!
What is the expected result?
out=A(any(ismember(A,B),2),:)

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!