How to compare two different cell arrays ?

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

 Akzeptierte Antwort

Kenneth Eaton
Kenneth Eaton am 8 Feb. 2011

11 Stimmen

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'

5 Kommentare

I want to get the common elements as my result. I want,
c = {'he', 'hate'}
Please help.
Shubham Manglam
Shubham Manglam am 14 Nov. 2018
thanks a lot Kenneth Eaton , it did save me pain of implementing two for loops and comparing elements one by one.
Suppose I have the following:
a = { 'the', 'he', 'hate'}
b = { 'he', 'hate', 'she' }
And I want to obtain something like the following:
c = 'the'
d = 'she'
Is it possible with
setdiff
?
c = setdiff(a,b);
d = setdiff(b,a);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Oleg Komarov
Oleg Komarov am 8 Feb. 2011

5 Stimmen

a = {'the', 'he', 'hate'};
b = {'he', 'hate'};
ismember(a,b)
Oleg
Remus
Remus am 11 Aug. 2011

0 Stimmen

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

10 Kommentare

Walter Roberson
Walter Roberson am 11 Aug. 2011
isequal() if you just want to compare the overall cells for equality.
Is the question about treating the cells as sets of non-trivial "objects" and finding the objects that match between the sets?
JLK93
JLK93 am 10 Dez. 2015
Walter, I would find this code useful if you can help?
fan feng
fan feng am 5 Mai 2016
Have you found the Function, either built-in or used-defined, for two cell arrays containing non-string element?
tannaz akbarpour
tannaz akbarpour am 22 Dez. 2016
I have the same problem as yours now. and have to compare two cells of values. Do you have any idea? or any piece of code?
isequal() if you just want to compare the overall cells for equality. If that is not what you want, then please explain further what you want the output to be. And remember you can use
cellfun(@isequal, FirstCellArray, SecondCellArray)
if you want a cell-by-cell response of whether the entries are exactly equal.
chocho
chocho am 8 Mär. 2017
Bearbeitet: Walter Roberson am 8 Mär. 2017
what if i have 2 cells: first cell of size 628*1 and the second cell 244*1 as follow:
cell1{k,1}={'tcaa-a6-2677';'tcca-a6-2681';'tcca-aa-3566';........};
cell2{k,1}={'TCCA-A6-2671-11A-01R-1758-07';'TCGA-A6-2677-11A-01R-0826-07';'TCGA-AA-3520-11A-01R-0721-07'; 'TCGA-AA-3566-11A-01R-1758-07';......}
*i tried to separate them by regexp'-' and compare between them and keep only the matched onces in cell1 otherwise delete , but i failed *
Stephen23
Stephen23 am 8 Mär. 2017
Bearbeitet: Stephen23 am 8 Mär. 2017
@chocho phD: Use cellfun and strncmpi. Easy.
If you asked this as a question I would show you how.
chocho
chocho am 9 Mär. 2017
ok i posted it as a question, could i have an answer? i want to return only the matched onces and delete others []
chocho
chocho am 10 Mär. 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! .

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by