Create new variable considering specific rows & columns of a cell-array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Maria
am 20 Jul. 2014
Beantwortet: Azzi Abdelmalek
am 20 Jul. 2014
I have a cell-array firstly composed by row 1 with years and column 1 (Y) with un-repeated values. To each element of the first column (y) correspond several codes that may change with time or even not be available (NaN). For example:
Input={ Y 2000 2001 2002 2003 2004 2005 2006 2007 2008
1 NaN NaN 33 33 33 33 33 NaN NaN
5 NaN 2 2 NaN NaN 2 64 64 64
13 NaN NaN NaN NaN NaN NaN 765 765 765};
I am trying to obtain a new variable that gives me for each value in column 1 (Y) the first and last years (row 1) in wich you observe a code. For example:
Output={ Y Start End Start2 End2
1 2002 2006
5 2001 2002 2005 2008
13 2006 2008 };
In case there are interruptions (row 3 of Input) I would like to consider extra columns (Start2 and End 2, row 3 of Output) that identify the years in which there is a code for the value in column 1 (Y).
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 20 Jul. 2014
A={ 'Y' 2000 2001 2002 2003 2004 2005 2006 2007 2008
1 NaN NaN 33 33 33 33 33 NaN NaN
5 NaN 2 2 NaN NaN 2 64 64 64
13 NaN NaN NaN NaN NaN NaN 765 765 765}
M=cell2mat(A(2:end,2:end));
d=cell2mat(A(1,2:end));
for k=1:size(M,1)
v=M(k,:);
idx=~isnan(v);
ii1=strfind([0 idx 0],[0 1]);
ii2=strfind([0 idx 0],[1 0])-1;
ii=[ii1;ii2];
ii=ii(:);
B(k,1:numel(ii))=num2cell(d(ii));
end
n=size(B,2)/2;
h=regexp(sprintf('start%d end%d ',repmat(1:n,2,1)),'\S+','match');
B=[A(:,1) [h;B]]
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical 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!