Create new variable considering specific rows & columns of a cell-array

조회 수: 2 (최근 30일)
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).

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 20일
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개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by