Difference between the arrays

2 views (last 30 days)
Ananya Malik
Ananya Malik on 11 Aug 2016
Commented: Ananya Malik on 11 Aug 2016
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 Comments
Ananya Malik
Ananya Malik on 11 Aug 2016
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.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 11 Aug 2016
Edited: KSSV on 11 Aug 2016
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 Comment
Ananya Malik
Ananya Malik on 11 Aug 2016
Thanks a lot. works perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!