removing repeats from lists

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

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)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 9 May 2022

Commented:

on 9 May 2022

Community Treasure Hunt

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

Start Hunting!