5

I have two arrays :

array_x = [x1, x2, x3, x4... xn]
array_y = [y1, y2, y3, y4... yn]

I would like to have a function f(array_x, array_y, value_x) that returns the value_y associated to the value_x by interpolation into the arrays.

How to do that ?

2
  • 1
    It is not entirely clear what you are looking for. Do you want the value from array_y that is in the position corresponding to where value_x is in array_x? Commented Mar 5, 2013 at 13:27
  • 1
    @Noio: Say you have values [1,2,3] at times [10,20,30], the idea is to iterpolate the value at say time 24. There are lots of ways to do this. The simplest is just a piecewise linear interpolation, which is what numpy.interp uses. Commented Mar 5, 2013 at 13:35

1 Answer 1

13

I think that numpy.interp is exactly what you want. e.g.:

numpy.interp(value_x,array_x,array_y)

Note that here value_x can be a scalar or another array-like value. If it is an array-like value, you will be returned an array of corresponding interpolated values.

Sign up to request clarification or add additional context in comments.

3 Comments

Hm... How to write this my own? Some people complain that they got no numpy, so I need this myself...
Linear interpolation is a pretty well known algorithm. From there, it's just a matter of searching the array (could use bisection) for the elements that bound the value where you want to interpolate to -- With that said, for any real mathematical analysis, numpy seems to be the standard. Is there a reason why those people can't install numpy?
When I make game, I wouldn't want to place a tester for numpy(if it is installed) and installer(if it is not). So...

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.