find similarities between cells

2 ビュー (過去 30 日間)
Richard
Richard 2012 年 3 月 31 日
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'};

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 3 月 31 日
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 件)

カテゴリ

Help Center および File ExchangeEnvironmental Models についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by