필터 지우기
필터 지우기

Difference between the arrays

조회 수: 5 (최근 30일)
Ananya Malik
Ananya Malik 2016년 8월 11일
댓글: Ananya Malik 2016년 8월 11일
I have 2 arrays A= [1 2 3 4] and B = [2 3 4 5] representing paths in a graph. I want to find difference between the arrays. A-B =[1 2] and B-A = [4 5]. Can any one help. Thanks in advance.
  댓글 수: 2
Cyrus
Cyrus 2016년 8월 11일
Hi,
Shouldn't A-B = [1] and B-A = [5] ?
to my understanding you want to keep the numbers that are not repeated in the other array, is that right?
Ananya Malik
Ananya Malik 2016년 8월 11일
actually A=[1 2 3 4] represents paths in a graph. 1-2, 2-3, 3-4. Similarly for B. By A-B, I want to print the edges in A but not in B.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2016년 8월 11일
편집: KSSV 2016년 8월 11일
clc ; clear all ;
A= [1 2 3 4];
B = [2 3 4 5] ;
A1 = [A(1:end-1) ; A(2:end)]' ;
B1 = [B(1:end-1) ; B(2:end)]' ;
AB = setdiff(A1,B1,'rows')
BA = setdiff(B1,A1,'rows')
  댓글 수: 1
Ananya Malik
Ananya Malik 2016년 8월 11일
Thanks a lot. works perfectly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by