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 CenterFile Exchange에서 Environmental Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by