【问题标题】:What is the equvalent interpolation from matlab in python?python中matlab的等效插值是什么?
【发布时间】:2020-10-17 22:58:50
【问题描述】:

我想将代码从 matlab 重写为 python。在matlab中我有以下内容:

interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time, 'next') % matlab

“下一个”是指哪个插值?通常默认是线性的。有一个 numpy 等价物吗?例如:

numpy.interp(x, xp, fp, left=None, right=None, period=None) # python

这是“线性”插值...

【问题讨论】:

  • 看一下scipy,它的插值函数应该支持'next'方法。

标签: python matlab numpy interpolation


【解决方案1】:

'next' 是指哪个插值?通常默认是线性的。有 numpy 等价物吗?

插值方法'next'插值到数据集中的下一个数据点(见:https://www.mathworks.com/help/matlab/ref/interp1.html)。

查看 NumPy 的文档 (https://numpy.org/doc/stable/reference/generated/numpy.interp.html),它们似乎使用线性插值,因此如果您想要相同的输出,只需在 MATLAB 命令中指定,如下所示:

interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time, 'linear')

话虽如此,'linear'interp1 的默认插值方法,因此您也可以简单地忽略该参数并使用以下命令:

interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time)

我希望这会有所帮助!

编辑:我刚刚意识到你在问的是你想要使用 Python 中的“next”方法进行内插。这是我的做法:

import numpy as np
import scipy as sp

# Generate data
x = np.linspace(0, 1, 10)
y = np.exp(x)

# Create interpolation function 
f = sp.interpolate.interp1d(x, y, 'next')

# Create resampled range
x_resampled = np.linspace(x[0], x[-1], 100)

# Here's your interpolated data
y_interpolated = f(x_resampled)

【讨论】:

  • 反过来。我想在 python 实现中有“下一个”。
  • 哦,我的错,我应该更仔细地阅读你的问题!请查看我对上面之前答案的编辑:)
猜你喜欢
  • 2015-09-09
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2014-12-04
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多