find similarities between cells
2 views (last 30 days)
Show older comments
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'};
0 Comments
Accepted Answer
Andrei Bobrov
on 31 Mar 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)
0 Comments
More Answers (0)
See Also
Categories
Find more on Environmental Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!