在带有NaN输入的Matlab中使用interp2 [英] Using interp2 in Matlab with NaN inputs

查看:982
本文介绍了在带有NaN输入的Matlab中使用interp2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matlab的矩阵中有一些比较完整的观测数据,但其中包含一些NaN值,我想使用interp2

I have some observational data that is relatively complete, but contains some NaN values, in an matrix in matlab and I want to interpolate them to a more evenly spaced grid using interp2

为了简单起见,假设我有一个完整的矩阵(没有NaN值),而另一个矩阵看起来像:

So, to keep things simple lets say I have one complete (no NaN values) matrix, and one that looks something like:

A = [ 1  2  3  4;
      2  3  2 NaN;
      0  2  3  4;
      0 NaN 4  5  ]

,其中BC是完整矩阵,interp2将不接受具有NaN值的输入矩阵.所以,如果我做这样的事情:

with B and C being complete matrices, interp2 won't accept an input matrix with NaN values. So if I do something like this:

[AI,BI] = meshgrid(a,b) %# matrices to interpolate data to, arbitrary
CI = interp2(A,B,C,AI,BI) %# interpolation, A has NaN values

我得到一个错误:

Error using griddedInterpolant
The coordinates of the input points must be finite values; Inf and NaN are not permitted.

任何人都可以提出解决方案或合理的解决方案,以免妨碍我的数据吗?

Can anyone suggest either a solution, or reasonable work around that doesn't obstruct my data?

推荐答案

很抱歉,我在注释中给出的快速修复不能直接用于2D数据(确实可以通过 interp1 不过,如果您需要的话).

Sorry the quick fix I gave in comment does not work directly for 2D data (it does work that simply with interp1 though, if you ever need it).

对于网格数据,如果网格中包含NaN,则您将没有统一的网格,并且不能直接使用interp2.在这种情况下,您必须先使用 griddata 重新插值您的数据在统一的网格上(基本上打了个洞).

For gridded data, if you have NaNs in your grid then you do not have a uniform grid and you cannot use interp2 directly. In this case you have to use griddata first, to re-interpolate your data over a uniform grid (patch the holes basically).

(1)让我们展示一个受Matlab文档启发的示例:

(1) Let's show an example inspired from the Matlab doc:

%% // define a surface
[A,B] = meshgrid(-3:0.25:3);
C = peaks(A,B);

%// poke some holes in it (in every coordinate set)
A(15,3:8)   = NaN ;
B(14:18,13) = NaN ;
C(8,16:21)  = NaN ;

(2)现在,让我们在干净的网格上修复数据:

(2) Now let's fix your data on a clean grid:

%// identify indices valid for the 3 matrix 
idxgood=~(isnan(A) | isnan(B) | isnan(C)); 

%// define a "uniform" grid without holes (same boundaries and sampling than original grid)
[AI,BI] = meshgrid(-3:0.25:3) ;

%// re-interpolate scattered data (only valid indices) over the "uniform" grid
CI = griddata( A(idxgood),B(idxgood),C(idxgood), AI, BI ) ;

(3)网格均匀后,如果要在更细的网格上进行网格划分,则可以使用interp2,例如:

(3) Once your grid is uniform, you can then use interp2 if you want to mesh on a finer grid for example:

[XI,YI] = meshgrid(-3:0.1:3) ;   %// create finer grid
ZI = interp2( AI,BI,CI,XI,YI ) ; %// re-interpolate

但是,请注意,如果这只是您要执行的操作,则也可以只使用griddata,然后一步一步完成所有操作:

However, note that if this is all what you wanted to do, you could also use griddata only, and do everything in one step:

%// identify indices valid for the 3 matrix 
idxgood=~(isnan(A) | isnan(B) | isnan(C)); 

%// define a "uniform" grid without holes (finer grid than original grid)
[XI,YI] = meshgrid(-3:0.1:3) ;

%// re-interpolate scattered data (only valid indices) over the "uniform" grid
ZI = griddata( A(idxgood),B(idxgood),C(idxgood), XI, YI ) ;

这将产生与我们在上面的步骤(3)中获得的网格和数据完全相同的网格和数据.

This produces the exact same grid and data than we obtained on step (3) above.

最后注意:如果您的NaN位于域的边界上,则默认情况下,这些函数无法为这些边界内插"值.要强制他们这样做,请查看这些函数的extrapolation选项,或简单地在边界较小的较小网格上进行插值,该网格上没有NaN.

Last note: In case your NaNs are on the border of your domain, by default these functions cannot "interpolate" values for these border. To force them to do so, look at the extrapolation options of these functions, or simply interpolate on a slightly smaller grid which doesn't have NaN on the border.

这篇关于在带有NaN输入的Matlab中使用interp2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆