I have a 3D array that I want to expand into a new 3D array. The data vectors in the 3rd axis are sorted data, corresponding to a fixed array of x coordinates. I want to expand the third axis along a new x coordinate array, with the values linearly interpolated from third axis of the original array, over all vectors in the original array. In other words, I have to apply linear interpolation many times to create the desired result. I am looking for a fast, possibly vectorized solution. An example using dummy data of the nested for-loop implementation is shown below. This solution is slow for as my original data set is very large
import numpy as np
fp = np.sort(np.random.rand(1000,100,10), axis = 2)
xp = np.linspace(0.0, 1.0, num=10)
x = np.linspace(0.0, 1.0, num=20)
result=np.zeros((1000,100,20))
for i in range(1000):
for j in range(100):
result[i,j,:] = np.interp(x,xp,fp[i,j,:])
Is there a faster, more efficient way to do this without the for loops?