comparing 2 arrays

조회 수: 10 (최근 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