How do I convert an array of numbers from an old range to a new range, where the highest valued number is 100 and the lowest valued number is a 0?

82 Views Asked by At

I am trying to develop weights for a mapping application of mine. I know it's a bit of a weird question, but say we have an array of some values like [1, 5, 7, 9, 3] I want the converted array to have the 9 be replaced with a 0 and the 1 replaced with a 100 as it's the lowest value. What would be the best way to approach this?

2

There are 2 best solutions below

2
On BEST ANSWER

I think what you're looking for is a min-max normalization. You want to normalize the array but instead of the usual range of 0-1 you want it to be from 0-100, but also reversed I suppose because you want 9 to be 0 and 1 to be 100.

For a value v in the array you have, it can be normalized on this new range via: $v’ = (v-min)/(max-min) * (newmax-newmin) + newmin$

$v$ is the old value and $v'$ is the new value.

Heres a Python function that would do what I believe you're looking for python

0
On

If you want a linear function that maps your minimum value to $0$, and your maximum value to $100$, use:

$$y = \frac{100}{\max - \min}(x - \min)$$ You can confirm directly that when you substitute $x = \min$ into this formula, it computes $y = 0$; and when you substitute $x = \max$ into this, it computes $y = 100$, as desired.

In the case where the values are $[1, 5, 7, 9, 3]$ you would use $$y = \frac{100}{6}(x - 3).$$