filling the gaps in the sequence of dates
조회 수: 1 (최근 30일)
이전 댓글 표시
EDITED
Dear all,
I have A={
'kl' '10/08' [4.4840] [4.1101] [ 0]
'kl' '01/09' [4.4840] [4.1101] [ 0]
'kl' '02/09' [4.1101] [4.0311] [ 0]
'kl' '03/09' [4.0311] [3.9358] [ 0]
'kl' '04/09' [3.9358] [3.9739] [ 0]
'kl' '05/09' [3.9739] [3.9267] [ 0]
'kl' '07/09' [3.9059] [3.8655] [ 0]
'kl' '08/09' [3.8655] [3.8889] [3.7498]
'kl' '10/09' [3.7498] [3.8857] [ 0]
'kl' '11/09' [3.8857] [4.4207] [4.1647]
'kl' '01/10' [4.1647] [3.7704] [ 0]
'kl' '02/10' [3.7495] [3.7085] [ 0]
'kl' '04/10' [3.7085] [3.6800] [ 0]
'kl' '05/10' [3.6800] [3.7364] [3.7867]
'kl' '07/10' [3.7867] [3.7860] [ 0]
'kl' '08/10' [3.7860] [3.7888] [3.6435]
'kl' '10/10' [3.6435] [3.6149] [ 0]
'kl' '11/10' [4.2260] [3.8786] [ 0]
'kl' '01/11' [3.8786] [3.5946] [3.5765]
'kl' '02/11' [3.5765] [3.5946] [ 0]
'kl' '04/11' [3.5946] [3.6493] [ 0]
'kl' '05/11' [3.6493] [3.5918] [3.6956]
'kl' '07/11' [3.6956] [3.7282] [ 0]
'kl' '08/11' [3.7326] [3.6308] [ 0]
'kl' '10/11' [3.6308] [3.6523] [4.1421]
'kl' '11/11' [4.1421] [2.0710] [ 0]}
The second column is month/year. Is it possible to fill the gaps in the sequence of the dates and for this additional row to set the rest of the elements equal to NaN? Specifically, the first date changes and is not fixed. Also the last date must be the date of the last row.
That is;
A={
'kl' '10/08' [4.4840] [4.1101] [ 0
[NaN] '11/08' [NaN] [NaN] [NaN]
[NaN] '12/08' [NaN] [NaN] [NaN]
'kl' '01/09' [4.4840] [4.1101] [ 0]
'kl' '02/09' [4.1101] [4.0311] [ 0]
'kl' '03/09' [4.0311] [3.9358] [ 0]
'kl' '04/09' [3.9358] [3.9739] [ 0]
'kl' '05/09' [3.9739] [3.9267] [ 0]
[NaN] '06/09' [NaN] [NaN] [NaN]
'kl' '07/09' [3.9059] [3.8655] [ 0]
'kl' '08/09' [3.8655] [3.8889] [3.7498]
[NaN] '09/09' [NaN] [NaN] [NaN]
'kl' '10/09' [3.7498] [3.8857] [ 0]
'kl' '11/09' [3.8857] [4.4207] [4.1647]
[NaN] '12/09' [NaN] [NaN] [NaN]
And so forth . the last date must be
'11/11'
Just to mentionthat the last date may change and is not fixed as it happens with the first date. SO the "last date" can be any date and the code must not create any new dates after the "last date"
Thanks in advance
댓글 수: 0
채택된 답변
Andrei Bobrov
2012년 8월 7일
편집: Andrei Bobrov
2012년 8월 8일
d0 = datenum(A(:,1),'mm/yy');
k = diff(year(d0([1,end]))) + 1;
d1 = datenum(2009,(1:k*12)',1);
out = num2cell(nan(numel(d1),size(A,2)));
out(:,1) = cellstr(datestr(d1,'mm/yy'));
out(ismember(d1,d0),2:end) = A(:,2:end);
EDIT
[y,m] = datevec(A([1,end],2),'mm/yy');
mths = diff(y)*12+diff(m);
N = cellstr(datestr(datenum(y(1),(m(1)+(0:mths))',1),'mm/yy'));
out = repmat({nan},numel(N),size(A,2));
out(ismember(N,A(:,2)),[1,3:end]) = A(:,[1,3:end]);
out(:,2) = N;
댓글 수: 5
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!