Create new variable considering specific rows & columns of a cell-array
3 次查看(过去 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).
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!