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 Kommentar

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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 9 Mai 2022

0 Stimmen

d=intersect(a,b);
d=d(~ismember(d,c));

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Kommentiert:

am 9 Mai 2022

Community Treasure Hunt

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

Start Hunting!

Translated by