Python 在向量的numpy数组中的每个向量之间创建插值向量?
我有一个创建插值点的函数,但我希望numpy有一个更好的方法来获得相同的结果。向量可以是2d空间中的任意位置,并且不可能是任何数字顺序(尽管我在示例中使用了arange()Python 在向量的numpy数组中的每个向量之间创建插值向量?,python,numpy,vector,interpolation,Python,Numpy,Vector,Interpolation,我有一个创建插值点的函数,但我希望numpy有一个更好的方法来获得相同的结果。向量可以是2d空间中的任意位置,并且不可能是任何数字顺序(尽管我在示例中使用了arange() import numpy as np def spread_array(ar=None, steps=3): """Create interpolated points between points in an array of vectors.""
import numpy as np
def spread_array(ar=None, steps=3):
"""Create interpolated points
between points in an array
of vectors."""
v = ar[1:] - ar[:-1]
div = v / (steps + 1)
new_ar = np.zeros((ar.shape[0] * (steps + 1), 2))
new_ar[::steps + 1] = ar
for i in range(steps):
ph = ar[:-1] + (div * (i + 1))
new_ar[:-(steps +1)][i+1::steps+1] = ph
return new_ar[: -steps]
xy = np.arange(4)
ar = np.zeros(8)
ar.shape = (4,2)
ar[:,0] = xy
ar[:,1] = xy
# or ar = np.random.rand(4,2)
new_ar = spread_array(ar)
print('------ new ------')
print(ar)
print(new_ar)