为什么Matlab interp1与numpy interp会产生不同的结果? [英] Why does Matlab interp1 produce different results than numpy interp?

查看:495
本文介绍了为什么Matlab interp1与numpy interp会产生不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:对代码进行了编辑,以产生与Matlab一致的结果.见下文.

Code edited to produce results consistent with Matlab. See below.

我正在将Matlab脚本转换为Python,并且线性插值结果在某些情况下会有所不同.我想知道为什么以及是否有任何方法可以解决此问题?

I am converting Matlab scripts to Python and the linear interpolation results are different in certain cases. I wonder why and if there is any way to fix this?

这是Matlab和Python中的代码示例以及产生的输出(请注意,在这种情况下t恰好等于tin):

Here is the code example in both Matlab and Python and the resulting output (Note that t just so happens to be equal to tin in this case):

MATLAB:

t= [ 736696., 736696.00208333, 736696.00416667, 736696.00625, 736696.00833333, 736696.01041667, 736696.0125];
tin =[ 736696., 736696.00208333, 736696.00416667, 736696.00625, 736696.00833333, 736696.01041667,  736696.0125];
xin = [   nan , 1392.,  1406. , 1418.  ,  nan , 1442. ,   nan];

interp1(tin,xin,t)

ans =

 NaN        1392        1406        1418         NaN        1442         NaN

Python(numpy):

(scipy interpolate.interp1d产生与numpy相同的结果)

(scipy interpolate.interp1d produces the same result as numpy)

t= [ 736696., 736696.00208333, 736696.00416667, 736696.00625, 736696.00833333, 736696.01041667, 736696.0125];
tin =[ 736696., 736696.00208333, 736696.00416667, 736696.00625, 736696.00833333, 736696.01041667,  736696.0125];
xin = [   nan , 1392.,  1406. , 1418.  ,  nan , 1442. ,   nan];

x = np.interp(t,tin,xin)

array([   nan,  1392.,  1406.,    nan,    nan,    nan,    nan]) 

# Edit
# Find indices where t == tin and if the np.interp output 
# does not match the xin array, overwrite the np.interp output at those 
# indices 
same = np.where(t == tin)[0]
not_same = np.where(xin[same] != x[same])[0]
x[not_same] = xin[not_same]

推荐答案

它看起来好像Matlab的插值中包含一个附加的相等性检查.

It appears as if Matlab includes an additional equality check in it's interpolation.

线性一维插值通常是通过找到两个跨越输入值x的x值,然后将结果计算为:

Linear 1-D interpolation is generally done by finding two x values which span the input value x and then calculating the result as:

y = y1 + (y2-y1)*(x-x1)/(x2-x1)

如果传入的x值精确等于输入的x坐标之一,则该例程通常将计算正确的值,因为x-x1为零.但是,如果输入数组的nany1y2,则这些将传播到结果中.

If you pass in an x value which is exactly equal to one of the input x coordinates, the routine will generally calculate the correct value since x-x1 will be zero. However, if your input array has a nan as y1 or y2 these will propagate to the result.

根据您发布的代码,我最好的猜测是Matlab的插值函数还有一个额外的检查项,例如:

Based on the code you posted, my best guess would be that Matlab's interpolation function has an additional check that is something like:

if x == x1:
    return y1

并且numpy函数没有此检查.

and that the numpy function does not have this check.

要在numpy中实现相同的效果,您可以执行以下操作:

To achieve the same effect in numpy you could do:

np.where(t == tin,xin,np.interp(t,tin,xin))

这篇关于为什么Matlab interp1与numpy interp会产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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