MATLAB Answers

filling the gaps in the sequence of dates

16 views (last 30 days)
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 Comments

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 7 Aug 2012
Edited: Andrei Bobrov on 8 Aug 2012
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 Comments

Show 2 older comments
Azzi Abdelmalek
Azzi Abdelmalek on 8 Aug 2012
you just modified his code, like i did with mine after your new observation.
antonet
antonet on 8 Aug 2012
ok Azzi. I left only the relevant parts. I hope this helps and sorry for causing any inconvenience. I am just struggling to find the solution and I can't. If you want please help me
thank you

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags