find similarities between cells

2 visualizaciones (últimos 30 días)
Richard
Richard el 31 de Mzo. de 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'};

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 31 de Mzo. de 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)

Más respuestas (0)

Categorías

Más información sobre Environmental Models en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by