# How to compare two different cell arrays ?

884 views (last 30 days)
vish on 8 Feb 2011
Commented: Walter Roberson on 24 May 2019
I would like to know if there is any way by which I can compare all the elements and remove the matched elements if not needed.
eg : a = the, he, hate
b = he, hate
so c should be equal to the.
PS: I have tried string comparision method but, unfortunately they eliminate the 'he' from 'the' thus giving the output as only 't'.
Thank you, vish
Igor Fedorov on 3 May 2016
Have you tried the function setdiff()?

Kenneth Eaton on 8 Feb 2011
The SETDIFF function does what you want. It will give you the values in one set (i.e. cell array) that are not present in another set:
>> a = {'the', 'he', 'hate'};
>> b = {'he', 'hate'};
>> c = setdiff(a,b)
c =
'the'
Walter Roberson on 24 May 2019
c = setdiff(a,b);
d = setdiff(b,a);

Oleg Komarov on 8 Feb 2011
a = {'the', 'he', 'hate'};
b = {'he', 'hate'};
ismember(a,b)
Oleg

Remus on 11 Aug 2011
What if I have two cell arrays with each array element continuing some structure with a combination of real elements and strings. i.e. A = {struct1,struct2,...} B = {struct1,struct2,...} here each struct can be of the type : structX.member1= ... (string/number) structX.member2= ... (string/number) ... Is there a way to compare the two cel arrays. dismember or setdiff only looks for strings so it won't work. Thanks Remus
chocho on 10 Mar 2017
Stephen Cobeldick thanks bro,but in my case i have a cell array inside another cell array i.e cell 1 627*1 (row=627 and col=1) and inside each row (from 1 to 627 ) i have 1*3 cell! .