comparing 2 arrays

12 visualizaciones (últimos 30 días)
Anand Anand
Anand Anand el 3 de Abr. de 2011
I have an array A=[1 2 3] B=[1 2 5] I want to get the elements that are present only in one of the arrays stored in A and B like A=[3] and B=[5] because 1 and 2 are present in both the arrays.How do I achieve that?

Respuestas (2)

Jan
Jan el 4 de Abr. de 2011
A = [1 2 3]
B = [1 2 5]
A2 = setdiff(A, B);
B2 = setdiff(B, A);
Or if you really need the same names as output:
A2 = setdiff(A, B);
B = setdiff(B, A);
A = A2;

Walter Roberson
Walter Roberson el 3 de Abr. de 2011
EDIT: corrected as per Jan's observation
u = intersect(A,B);
A = setdiff(A,u);
B = setdiff(B,u);
  1 comentario
Jan
Jan el 4 de Abr. de 2011
SETINTERSECT? I assume you mean INTERSECT.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by