How to find interpolation of x in a function of y by using intrep1 ?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
So I'm trying to understand how interp1 works in Matlab. I have two variables x and y. I have NaN values in x and I have y values. So I want to find NaN values in x by using y.
I found this code for interp1.
nanx = isnan(x);
t = 1:numel(x);
x(nanx) = interp1(t(~nanx), x(~nanx), t(nanx));
However in this case from what I understand it interpolate based on the previous value i x list not based on y values.
I really hope someone can explain this to me.
0 comentarios
Respuestas (2)
Jan
el 31 de En. de 2018
You are right: In this example the NaNs are treated as gaps, which are filled by a linear interpolation of the surrounding points. An y does not appear in the code and therefore it does not solve your problem.
I have NaN values in x and I have y values.
So I want to find NaN values in x by using y.
This is not clear yet. To find NaN values in x use: isnan(x). I do not see a method to use another variable y to find NaNs in x. Please explain this again. Post an example:
x = [1, 2, 3, 4, NaN, NaN, 8, 10]
y = [0, 1, 4, 5, 7, 11, 3, 2]
So what is the wanted result?
2 comentarios
Stephen23
el 31 de En. de 2018
@Ss: please take a look at the example x and y vectors in Jan Simon's answer. Please shows the exact vector you would expect the output to be, given those two input vectors.
Walter Roberson
el 31 de En. de 2018
Editada: Walter Roberson
el 31 de En. de 2018
Is y monotonic, either constantly increasing or constantly decreasing? If so then you can reverse the order of interpolation.
x_nan_mask = isnan(x);
y_at_nan = y(x_nan_mask);
x_no_nan = x(~x_nan_mask);
y_no_nan = y(~x_nan_mask);
x_reconstructed_at_nan = interp1(y_no_nan, x_no_nan, y_at_nan);
x_reconstructed = x;
x_reconstructed(x_nan_mask) = x_reconstructed_at_nan;
If y is not monotonically increasing then there is no unique solution unless you assume that it is piecewise continuous and that the x that are nan are in the interior (not boundary) of pieces. In such a case the solution is to break the problem up into sections that are individually piecewise and then reverse the order of interpolation.
2 comentarios
Walter Roberson
el 31 de En. de 2018
interp1() can only work for monotonic increasing or decreasing data.
You could certainly write a loop that stepped through a point at a time, detected nan, and interpolated from the neighbours.
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!