comparing 2 arrays

2 次查看(过去 30 天)
Anand Anand
Anand Anand 2011-4-3
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?

回答(2 个)

Jan
Jan 2011-4-4
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 2011-4-3
EDIT: corrected as per Jan's observation
u = intersect(A,B);
A = setdiff(A,u);
B = setdiff(B,u);
  1 个评论
Jan
Jan 2011-4-4
SETINTERSECT? I assume you mean INTERSECT.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by