-1

I have the following array:

a = np.array([20, 10, 5, 15])

Given a new interval (for instance B = [10, 40]) I need to spread the values of the original array an the new interval, so the result would be:

b = np.array([40., 20, 10., 30])

A possible solution would be applying the following formula (in pseudocode) to each value:

new_value = min(B) + (current_value - min(a)) * (max(a)-min(a)) / (max(B)-min(B))

Any quicker approach ?

Nico
  • 113
  • 8

1 Answers1

0

As mentioned in the comments, you can use numpy.interp like so:

np.interp(a, (a.min(),a.max()), (10,40))
rikyeah
  • 1,896
  • 4
  • 11
  • 21