添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

我尝试在Python中使用NumPy做同样的插值。我首先对行进行内插,然后对列进行内插

import numpy as np
ax = np.array([0.0, 0.2, 2.0])
ay = np.array([0.0, 0.2, 2.0])
I = np.array([[10,20,30,40,50]])
I = np.concatenate((I,I,I,I,I), axis=0)
r_idx = np.arange(1, I.shape[0]+1)
c_idx = np.arange(1, I.shape[1]+1)
I_row = np.transpose(np.array([np.interp(ax, r_idx, I[:,x]) for x in range(0,I.shape[0])]))
I_col = np.array([np.interp(ay, c_idx, I_row[y,:]) for y in range(0, I_row.shape[0])])
SI = I_col

However, the resulting SI is

10 10 20 10 10 20 10 10 20

为什么我使用Python的结果与使用MATLAB的结果不同?

python
matlab
numpy
interpolation
Wkjung
Wkjung
发布于 2020-09-08
1 个回答
lpeak
lpeak
发布于 2020-09-08
已采纳
0 人赞同

从MATLAB到Python的传递,似乎是你自己纠正过头了,正如你的第一个代码摘录所示。

ax = [0, 0.2, 2] #start from index 0: python

在numpy逻辑中,这个序列并不代表索引,而是代表函数插值的坐标 用于插值的函数。 因为你已经处理了坐标的递增问题,以便与matlab兼容。

r_idx = np.arange(1, I.shape[0]+1)
c_idx = np.arange(1, I.shape[1]+1)

你可以重复使用你在Matlab中使用的插值坐标。

ax = [1,1.2,3]

Full code:

import numpy as np
ax = np.array([1.0, 1.2, 3.0])
ay = np.array([1.0, 1.2, 3.0])
I = np.array([[10,20,30,40,50]])
I = np.concatenate((I,I,I,I,I), axis=0)
r_idx = np.arange(1, I.shape[0]+1)
c_idx = np.arange(1, I.shape[1]+1)
I_row = np.transpose(np.array([np.interp(ax, r_idx, I[:,x]) for x in range(0,I.shape[
0])]))
I_col = np.array([np.interp(ay, c_idx, I_row[y,:]) for y in range(0, I_row.shape[0])]
SI = I_col
array([[10., 12., 30.],
       [10., 12., 30.],