Search code examples
matlabcubic-spline

Spline Interpolation Matlab


I have the following:

x = 1:365;
y = T;
xx = missing;
yy = spline(x,y,xx)

I have data T, which is 365 days of data, and missing is a vector containing the days on which the data is faulty. I need to generate estimated values at the missing days. However, when I use the above syntax, it returns a vector of 0s. What am I doing wrong?


Solution

  • I suspect the missing data are specified in your vector T as zeros. You give that information to spline to interpolate, and that's easy, the exact value is already there, it's a zero, and that is what is returned. Assuming that missing contains the day numbers where data is missing, try this:

    x = 1:365;
    y = T;
    x(missing) = [];
    y(missing) = [];
    xx = missing;
    yy = spline(x,y,xx)
    

    This way the missing data are not encoded as zeros anymore, but they are actually missing, and spline can use the neighbors to estimate values.