Modified linear interpolation with missing data
-
27-06-2021 - |
Domanda
Imagine a set of data with given x-values (as a column vector) and several y-values combined in a matrix (row vector of column vectors). Some of the values in the matrix are not available:
%% Create the test data
N = 1e2; % Number of x-values
x = 2*sort(rand(N, 1))-1;
Y = [x.^2, x.^3, x.^4, x.^5, x.^6]; % Example values
Y(50:80, 4) = NaN(31, 1); % Some values are not avaiable
Now i have a column vector of new x-values for interpolation.
K = 1e2; % Number of interplolation values
x_i = rand(K, 1);
My goal is to find a fast way to interpolate all y-values for the given x_i values. If there are NaN values in the y-values, I want to use the y-value which is before the missing data. In the example case this would be the data in Y(49, :)
.
If I use interp1
, I get NaN-values and the execution is slow for large x
and x_i
:
starttime = cputime;
Y_i1 = interp1(x, Y, x_i);
executiontime1 = cputime - starttime
An alternative is interp1q
, which is about two times faster.
What is a very fast way which allows my modifications?
Possible ideas:
- Do postprocessing of
Y_i1
to eliminateNaN
-values. - Use a combination of a loop and the
find
-command to always use the neighbour without interpolation.
Soluzione
Using interp1
with spline interpolation (spline
) ignores NaN's.