所以我有一个数据列“trainData
”,如下所示:
[
Nan
Nan
Nan
110
NaN
89
Nan
Nan
123
and so on
]
我基本上需要对此进行插值,以便得到最终的矩阵列:
[
0
36.6
73.2
110
99.5
89
101.3
112.6
123
and so on
]
任何人都可以帮我怎么做?
我尝试过做interpl(traindata)
,但要么给我一些奇怪的NaN行,要么就行不通。请帮帮我。
答案 0 :(得分:6)
y = [NaN
NaN
NaN
110
NaN
89
NaN
NaN
123];
以下内容非常接近(我认为你实际上发了一个算术错误,这就是你想要的):
y(1) = 0; %//I'm assuming this from your result, you gave us no information about this.
xi = (1:length(y))'; %'//Now I'm assuming that each element of your y matrix is equally spaced
x = xi(~isnan(y)); %// Find the x values that correspond to the numerical values of y
yi = interp1(x, y(~isnan(y)), xi)
yi =
0
36.6667
73.3333
110.0000
99.5000
89.0000
100.3333
111.6667
123.0000