필터 지우기
필터 지우기

Interpolating NaN-s

조회 수: 172 (최근 30일)
Karlito
Karlito 2012년 4월 3일
댓글: Matteo Soldini 2020년 2월 13일
I have a time series, where there are some missing values. I've marked them as NaN. How would it be possible for me to interpolate them from the rest of the data. So my data to interpolate looks like that (just example numbers):
x=
0.482230405436799
0.0140930751890233
0.622880344434796
NaN
0.527433962776634
0.724991961357239
0.607415791144935
0.588366445795251
0.433434840033058
0.244172893507025
0.428960353778007
0.0101774555217771
0.608821449044360
0.957975188197358
NaN
0.0355905633801477
0.886235111325751
0.246941365057497
0.00891505625333700
0.814920271448039
0.140499436548767
0.879866440284003
0.0953767860905247
0.352560101248640
How to get a value for those NaN-s? I've tried Interp1 but i can't get it working properly.
  댓글 수: 1
Jan
Jan 2012년 4월 3일
It is a good idea to post your trials with INTERP1, such that the problem can be fixed. This is usually easier that creating a solution from the scratch and you could learn, what went wrong.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 4월 3일
inn = ~isnan(x);
i1 = (1:numel(x)).';
pp = interp1(i1(inn),x(inn),'linear','pp')
out = fnval(pp,linspace(i1(1),i1(end),1000));
plot(i1,x,'ko',linspace(i1(1),i1(end),1000),out,'b-')
grid on

추가 답변 (2개)

Jan
Jan 2012년 4월 3일
nanx = isnan(x);
t = 1:numel(x);
x(nanx) = interp1(t(~nanx), x(~nanx), t(nanx));
This does not catch NaNs at the margins, such that you have to care for them by your own, e.g. by repeating the first or last non-NaN value.
  댓글 수: 8
Steven Lord
Steven Lord 2017년 8월 20일
Check if the fillmissing function does what you want.
Matteo Soldini
Matteo Soldini 2020년 2월 13일
If I have 50 variables and I use Jan's code to interpolate each nan in each variable (variables have different ranges of values, for example var1 = [1,2], var2 = [300,400]), will it work or should I use a for loop?

댓글을 달려면 로그인하십시오.


John D'Errico
John D'Errico 2012년 4월 3일
My inpaint_nans will fill them in with interpolated values, even if there are multiple consecutive nans, and it will extrapolate nicely at the ends. It works on 1-d problems.
x = inpaint_nans(x);
  댓글 수: 1
Jan
Jan 2012년 4월 3일
+1. And it works on >1 D problems, where our direct INTERP1 approachs fail.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interpolation of 2-D Selections in 3-D Grids에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by