Matlab“interp2”关于边缘处 NaN 的问题
我对一个简单的练习有点困惑,希望得到一些帮助。
我正在尝试使用Matlab中的“interp2”函数对尺寸为[15x12]的变量“tmin”进行一些简单的2D插值:
lat = 15:1.5:32;
lon = 70:1.5:92;
lat_interp = 15:1:32;
lon_interp = 70:1:92;
[X,Y] = meshgrid(lat,lon);
[Xi,Yi] = meshgrid(lat_interp,lon_interp);
tmin_interp = zeros(length(lon_interp),length(lat_interp),Num_Days);
tmin_interp(:,:) = interp2(X,Y,tmin(:,:),Xi,Yi,'linear');
此代码导致tmin_interp的最后一行和最后一列为NaN,即:
tmin_interp(23,1:18) ==> NaN
tmin_interp(1:23,18) ==> NaN
有谁知道我可能做错了什么?我在插值设置方面犯了一个简单的错误吗?感谢您抽出时间。
I am a bit stuck on a simple exercise and would appreciate some help.
I am trying to do some simple 2D interpolation using the "interp2" function in Matlab for a variable 'tmin' of dimension [15x12]:
lat = 15:1.5:32;
lon = 70:1.5:92;
lat_interp = 15:1:32;
lon_interp = 70:1:92;
[X,Y] = meshgrid(lat,lon);
[Xi,Yi] = meshgrid(lat_interp,lon_interp);
tmin_interp = zeros(length(lon_interp),length(lat_interp),Num_Days);
tmin_interp(:,:) = interp2(X,Y,tmin(:,:),Xi,Yi,'linear');
This code results in the last row and last column of tmin_interp to be NaNs, i.e.:
tmin_interp(23,1:18) ==> NaN
tmin_interp(1:23,18) ==> NaN
Does anyone know what I might be doing wrong? Am I making a simple mistake with regards to the interpolation setup? Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们是
nan
的原因是网格前后没有数据可供插值。线性插值使用Xi,Yi
处的场梯度来估计该点的值。如果双方都没有的话,就不能。您可以使用
extrapval
参数在您指定的X,Y
之外进行推断。只需在'线性'
之后添加参数0
:这将为“边缘”的点置零。然而,对于外部的点,它们可能会下降到某个默认值,例如零。为此,您可以在
tmin
之前和之后添加零:(尚未检查这一点,但您明白了。)您还需要向
添加一些前置值和后置值X 和 Y。
如果这是 tmin 的“外部”或“默认”值,请使用其他值。
PS 为什么要创建 tmin_interp 为 3 维?
The reason they are
nan
s is that there is no data before and after your grid to interpolate to. Linear interpolation uses the gradient of the field at theXi,Yi
, in order to estimate the value at that point. If there is nothing either side, it can't.You can use
extrapval
parameter to extrapolate outside theX,Y
you specify. Just add the parameter0
after'linear'
:This will put zero for the points 'on the edge'. However, it is likely that for points outside, they may fall off to some default value, like zero. To do this, you can add zeros before and after
tmin
:(haven't checked this but you get the idea.) you will also need to add some pre- and post-values to
X
andY
.Use some other value, if that's the 'outside' or 'default' value of tmin.
PS why are you creating
tmin_interp
as 3-dimensional?或者只是尝试:
避免强加 0 值。
哈!
Or just try:
to avoid imposing the 0 value.
HTH!