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

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 8 de Mzo. de 2016
Editada: Azzi Abdelmalek el 8 de Mzo. de 2016
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 comentarios

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

N.
el 8 de Mzo. de 2016

Comentada:

el 8 de Mzo. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by