在 scipy 0.14 或更高版本中,有一个新函数scipy.interpolate.RegularGridInterpolator https://docs.scipy.org/doc/scipy-0.18.0/reference/generated/scipy.interpolate.RegularGridInterpolator.html#scipy.interpolate.RegularGridInterpolator这非常类似于interp3
.
MATLAB 命令Vi = interp3(x,y,z,V,xi,yi,zi)
会翻译成这样:
from numpy import array
from scipy.interpolate import RegularGridInterpolator as rgi
my_interpolating_function = rgi((x,y,z), V)
Vi = my_interpolating_function(array([xi,yi,zi]).T)
这是一个完整的例子,展示了两者;它将帮助您了解确切的差异...
MATLAB代码:
x = linspace(1,4,11);
y = linspace(4,7,22);
z = linspace(7,9,33);
V = zeros(22,11,33);
for i=1:11
for j=1:22
for k=1:33
V(j,i,k) = 100*x(i) + 10*y(j) + z(k);
end
end
end
xq = [2,3];
yq = [6,5];
zq = [8,7];
Vi = interp3(x,y,z,V,xq,yq,zq);
结果是Vi=[268 357]
这确实是这两点的值(2,6,8)
and (3,5,7)
.
SIPY代码:
from scipy.interpolate import RegularGridInterpolator
from numpy import linspace, zeros, array
x = linspace(1,4,11)
y = linspace(4,7,22)
z = linspace(7,9,33)
V = zeros((11,22,33))
for i in range(11):
for j in range(22):
for k in range(33):
V[i,j,k] = 100*x[i] + 10*y[j] + z[k]
fn = RegularGridInterpolator((x,y,z), V)
pts = array([[2,6,8],[3,5,7]])
print(fn(pts))
又是[268,357]
。所以你会看到一些细微的差别:Scipy 使用 x,y,z 索引顺序,而 MATLAB 使用 y,x,z (奇怪);在 Scipy 中,您在单独的步骤中定义一个函数,当您调用它时,坐标会像 (x1,y1,z1),(x2,y2,z2),... 那样分组,而 matlab 使用 (x1,x2,.. .),(y1,y2,...),(z1,z2,...)。
除此之外,两者相似并且同样易于使用。