How to find interpolation of x in a function of y by using intrep1 ?

4 Ansichten (letzte 30 Tage)
Ss
Ss am 31 Jan. 2018
Kommentiert: Stephen23 am 31 Jan. 2018
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));
from this link : Interpolating NaN-s
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.

Antworten (2)

Jan
Jan am 31 Jan. 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 Kommentare
Ss
Ss am 31 Jan. 2018
The answers for NaN values in x supposed to be 3 and -2 because all point x are dependent on y and calculated by using the linear interpolation method. The NaN values won't be regulated on x only but it also on y.
I'm sorry I'm not really familiar with MATLAB's code yet, mathematically the formula somehow like this:
xnan = x1 + (y-y1)*(x2-x1)/(y2-y1)
But I don't know how to apply it in intero1 and isnan(x). Do you get what I mean?
Stephen23
Stephen23 am 31 Jan. 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.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 31 Jan. 2018
Bearbeitet: Walter Roberson am 31 Jan. 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 Kommentare
Ss
Ss am 31 Jan. 2018
yes it is piecewise continuous and x are the nan.
Can't I just try to find NaN values of x and interpolate based on y using interp1 directly?
Walter Roberson
Walter Roberson am 31 Jan. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by