find similarities between cells

2 Ansichten (letzte 30 Tage)
Richard
Richard am 31 Mär. 2012
Consider the following example:
clear all
Year1a = {'Y2007','Y2008','Y2009','Y2010','Y2011'};
Year1b = {'Y2004','Y2005','Y2006','Y2007','Y2008','Y2009'};
Year1c = {'Y2007','Y2008','Y2009','Y2010','Y2011'};
Year1 = {Year1a,Year1b,Year1c};
Year2a = {'Y2005','Y2006','Y2007','Y2008','Y2009','Y2010','Y2011'};
Year2b = {'Y2005','Y2006','Y2007','Y2008','Y2009','Y2010','Y2011'};
Year2c = {'Y2005','Y2006','Y2007','Y2008','Y2009','Y2010','Y2011'};
Year2 = {Year2a,Year2b,Year2c};
I would like to compare the outputs from Year1 and Year2 and produce a third cell array which shows which Years are identical between Year1 and Year2. So, the outcome I would expect from this example would be:
Year{1} = {'Y2007','Y2008','Y2009','Y2010','Y2011'};
Year{2} = {'Y2005','Y2006','Y2007','Y2008','Y2009'};
Year{3} = {'Y2007','Y2008','Y2009','Y2010','Y2011'};

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 31 Mär. 2012
Year1 = {{'Y2007','Y2008','Y2009','Y2010','Y2011'}; {'Y2004','Y2005','Y2006','Y2007','Y2008','Y2009'};{'Y2007','Y2008','Y2009','Y2010','Y2011'}};
Year2 = {{'Y2005','Y2006','Y2007','Y2008','Y2009','Y2010','Y2011'}; {'Y2005','Y2006','Y2007','Y2008','Y2009','Y2010','Y2011'}; {'Y2005','Y2006','Y2007','Y2008','Y2009','Y2010','Y2011'}};
Year = cellfun(@intersect,Year1,Year2,'un',0)
OR in this is case
Year1 = {{'Y2007','Y2008','Y2009','Y2010','Y2011'}; {'Y2004','Y2005','Y2006','Y2007','Y2008','Y2009'};{'Y2007','Y2008','Y2009','Y2010','Y2011'}}
Year2 = {'Y2005','Y2006','Y2007','Y2008','Y2009','Y2010','Y2011'};
Year = cellfun(@(x)intersect(x,Year2),Year1,'un',0)

Weitere Antworten (0)

Kategorien

Mehr zu Environmental Models finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by