how to compare 2 or more cell arrays?

Hi, i have 2 cells(cell1 and cell2) of size 628*1 and 244*1 respectively, and i want to compare the elements of cell 1 with the elements of cell 2 and return only the matched ones from cell1 and delete the rests
cell1{k,1}={' *tcaa-a6-2677*';'tcca-a6-2681';' *tcca-aa-3566*';............};
%
cell2{k,1}={'TCCA-A6-2671-11A-01R-1758-07';' *TCCA-A6-2677*-11A-01R-0826-07';'TCCA-AA-3520-11A-01R-0721-07'; 'TCCA-AA-3528-11A-01R-1758-07'; *TCCA-AA-3566*-01A-01R-0125-07';.................}
Hints: i tried strncmpi and length, but because of size issue i get confused about which Command statement is better in my case.

 Risposta accettata

Stephen23
Stephen23 il 9 Mar 2017
Modificato: Stephen23 il 9 Mar 2017
After removing the asterisks, space characters, and fixing the first element of C1, this seems to do the job:
>> C1 = {'tcca-a6-2677';'tcca-a6-2681';'tcca-aa-3566'};
>> C2 = {'TCCA-A6-2671-11A-01R-1758-07';'TCCA-A6-2677-11A-01R-0826-07';'TCCA-AA-3520-11A-01R-0721-07';'TCCA-AA-3528-11A-01R-1758-07';'TCCA-AA-3566-01A-01R-0125-07'};
>> fun = @(s)any(strncmpi(s,C2,numel(s)));
>> out = C1(cellfun(fun,C1))
out =
'tcca-a6-2677'
'tcca-aa-3566'

8 Commenti

Hi Stephen Cobeldick, i got this Error:
Error using strncmpi
Inputs must be the same size or either one can be a scalar.
Error in @(s)any(strncmpi(s,D2,numel(s)))
Error in common_Patients (line 15) out = UD1(cellfun(fun,UD1));
Actually i want to use a loop instead but i don't know how to figure out this idea
Stephen23
Stephen23 il 10 Mar 2017
Modificato: Stephen23 il 10 Mar 2017
@chocho: do the cell arrays contain nested cell arrays?
I am happy to help you but guessing exactly how your data is arranged will just waste everyone's time. Please make a comment and upload your data by clicking the paperclip button.
then i tried your command and others like intersect ,strcmp and == but no result
here are the 2 cells
Stephen23
Stephen23 il 10 Mar 2017
Modificato: Stephen23 il 10 Mar 2017
>> fun = @(s)any(strncmpi(s,expl,numel(s)));
>> out = Vpl(cellfun(fun,Vpl))
out =
'tcga-a6-2677'
'tcga-a6-2682'
'tcga-aa-3516'
'tcga-aa-3520'
'tcga-aa-3521'
'tcga-aa-3522'
'tcga-aa-3524'
'tcga-aa-3525'
'tcga-aa-3527'
'tcga-aa-3530'
'tcga-aa-3531'
'tcga-aa-3543'
etc
Out of the 244 strings in expl, 229 can be matched to some string in Vpl. Is that what you expect?
Great!
sorry if i bother you, may be i didn't state clearly the problem
Appreciate your help
JR
JR il 8 Ott 2018
Modificato: JR il 8 Ott 2018
Awesome. Thanks Stephen, I had no idea about function handles using @ command.
Hello,
I have a similar need. I have 2 cells(cell1 and cell2) of size 72*1 and 110*1 respectively, and i want to compare the elements of cell 1 with the elements of cell 2 and return only the matched ones from cell1 and delete the rest.
The above mentioned solution is not working for me.
Can you please help me out with this ?

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 9 Mar 2017
Modificato: KSSV il 9 Mar 2017
cell1 = {'apple' 'boy' 'cat' 'dog' 'elephant'} ;
cell2 = {'apple' 'ball' 'cut' 'dog' 'egg' 'frog' 'gun'} ;
iwant = intersect(cell1,cell2) ;

9 Commenti

No doesn't work ,return an empty cell
You got empty cell with the above example? Or with your data?
with my data
KSSV
KSSV il 9 Mar 2017
Modificato: KSSV il 9 Mar 2017
Well it is because of different letter cases and text completely different. You see from cell1 you got tcaa-a6-2677 and cell2 you got TCCA-A6-2671-11A-01R-1758-07 only a part is in match. You have all strings like this or there would be an exact strings?
chocho
chocho il 9 Mar 2017
Modificato: per isakson il 10 Mar 2017
yes exactly as you see
all the strings like this and i just post an example but 244*1 and 628*1 cells format same as i post .
because i put them in an column vector so i'm finding it difficult to make a comparison between them
Are you okay if a part matches? I mean tcaa-a6-2677 and TCCA-A6-2671-11A-01R-1758-07 are same?
not same only the 3 first parts if they match then ok if not then delete
tcaa-a6-2677 and TCCA-A6-2677-11A-01R-0826-07
you can see in this example the bold parts are match
but in tcaa-a6-2677 and TCCA-A6-2671-11A-01R-1758-07 there is no match (even tcca-a6- match with TCCA-A6 but 2677 and 2671 not), so no match
i think you match element by element but i want to check every element in cell1 with all the elements in cell 2.
chocho
chocho il 9 Mar 2017
Modificato: per isakson il 10 Mar 2017
cell1{k,1}={' *tcaa-a6-2677*';'tcca-a6-2681';' *tcca-aa-3566*';............};
%
cell2{k,1}={'TCCA-A6-2671-11A-01R-1758-07';' *TCCA-A6-2677*-11A-01R-0826-07';'TCCA-AA-3520-11A-01R-0721-07'; 'TCCA-AA-3528-11A-01R-1758-07'; *TCCA-AA-3566*-01A-01R-0125-07';.................}
in this example elements 1 & 3 of cell 1 are match with the elements 2 & 5 of cell2

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by