5

Question: How do you you linearly interpolate each row of a 2d array with the same 1d array?

I have 2 sets of x-coordinates and a matrix of y-coordinates, I want to do this without the loop:

for k in range(len(y[:,0]))
    y_want = np.interp(x_want,x_have,y_have[k,:])
    y_new.append(y_want)

Is there a built in numpy function that can do this?

1 Answer 1

3

Use scipy.interpolate.interp1d:

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

x=np.linspace(0,1,11)

y=np.vstack((np.sin(x),np.cos(x),np.log1p(x)))

xi=np.linspace(0,1,101)

intf= interp1d(x,y,axis=1)

yi=intf(xi)

plt.ioff()
plt.plot(x,y.T,'x',
         xi,yi.T)
plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

For some reason this took longer than my loop. But, it is possible I am doing something wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.