removing repeats from lists

1 view (last 30 days)
Christabelle Miels
Christabelle Miels on 9 May 2022
Commented: Stephen23 on 9 May 2022
Hi, I'm struggling to figure out an output list 'd' which has all elements that are in both lists 'a' and 'b', but not in list 'c' using below:
a=[3 5 1 8]
b=[1 5 2]
c=[5 1 4 2]
d=[ ]
but this code only works when removing 'b' from 'a' to make list 'c'.
a=[3 5 1 8];
b=[1 5 2]
n=length(a); m=length(b); c=[ ];
for j=1:n
flag = 0;
k=0;
while flag == 0 && k
k=k+1;
if a(j) == b(k)
flag = 1;
end;
end;
if flag == 0
c=[c a(j)];
end;
end;
c
  1 Comment
Stephen23
Stephen23 on 9 May 2022
Not many elements:
a = [3,5,1,8];
b = [1,5,2];
c = [5,1,4,2];
d = setdiff(intersect(a,b),c)
d = 1×0 empty double row vector

Sign in to comment.

Answers (1)

David Hill
David Hill on 9 May 2022
d=intersect(a,b);
d=d(~ismember(d,c));

Categories

Find more on Graphics Object Programming 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!