MATLAB: Observations every 8 or 9 weeks-interpolation

interpolation

I have the cell matrix
A={
'28/12/08' [2.3187] [1.1517]
'01/03/09' [2.2314] [1.1540]
'03/05/09' [2.2094] [1.1702]
'28/06/09' [2.2666] [1.1762]
'30/08/09' [2.2240] [1.1860]
'01/11/09' [2.2387] [1.1741]
'03/01/10' [2.3229] [1.1729]
'07/03/10' [2.2253] [1.1559]
'09/05/10' [2.2504] [1.1678]
'04/07/10' [2.2673] [1.1810]
'05/09/10' [2.1828] [1.1816]
'07/11/10' [2.1271] [1.1660]
'02/01/11' [2.3143] [1.1674]
'06/03/11' [2.1860] [1.1544]
'08/05/11' [2.1770] [1.1704]
'03/07/11' [2.1832] [1.1758]
'04/09/11' [2.1578] [1.1766]
'06/11/11' [2.1612] [1.1710]}
the observations represent a 8 or 9 week average. And I want to convert these values to estimated monthly averages via interpolation.My thought is the following
%convert the mm/yy to number dates
xi=datenum(A(:,1),'mm/yy')
B=cell2mat(A(:,[2,3]));
for c = 2:3
B(:,c-1) = interp1(xi(1:2:end),cell2mat(A(:,c)),xi);
end
But I have the feeling that my approach is not correct. Any suggestions?
thank you in advance

Best Answer

  • One way:
    d1 = datenum(A(:,1),'dd/mm/yy');
    [y,m,d] = datevec(d1(1));
    d2 = datenum(y,m,d+ (0:diff(datenum(A([1,end],1),'dd/mm/yy')))');
    v = arrayfun(@(x)interp1(d1,cell2mat(A(:,x)),d2),2:size(A,2),'un',0);
    [a,b] = datevec(d2);
    [a1,b1,c] = unique([a,b],'rows');
    [x,y] = ndgrid(c,1:size(A,2)-1);
    out1 = accumarray([x(:),y(:)], cat(1,v{:}),[],@(x){mean(x)});
    out = [cellstr(datestr(d2(b1),'mm/yy')),out1];
    second way:
    d1 = datenum(A(:,1),'dd/mm/yy');
    [y,m,d] = datevec(d1([1,end]));
    d3 = datenum(y(1),m(1)+(1:diff([y,m])*[12;1]+1)',1)-1;
    out = [cellstr(datestr(d3,'mm/yy')), cell(numel(d3),size(A,2)-1)];
    for j1 = 2:size(out,2)
    out(:,j1) = num2cell(interp1(d1,cell2mat(A(:,j1)),d3,'linear','extrap'));
    end
    etc.