scale a number between a range [duplicate]

I have been trying to achieve a system which can scale a number down and in between two ranges. I have been stuck with the mathematical part of it.

What im thinking is lets say number 200 to be normalized so it falls between a range lets say 0 to 0.66 or 0.66 to 1 or 1 to 1.66. The range being variable as well.

Any help would be appreciated.

Thanks

• Do you know the (theoretical) minimum and maximum of the original values? Or can we use the entire range of available values to obtain these values (e.g. would you accept a max(x) and min(x) in 'the maths')? – IWS May 23 '17 at 9:07
• yes, That would involve a couple of additional loops but can be found. – Saneesh B May 23 '17 at 9:08
• There are many more duplicates. Here's a search: stats.stackexchange.com/…. – whuber May 23 '17 at 13:39

Your scaling will need to take into account the possible range of the original number. There is a difference if your 200 could have been in the range [200,201] or in [0,200] or in [0,10000].

So let

• $r_{\text{min}}$ denote the minimum of the range of your measurement
• $r_{\text{max}}$ denote the maximum of the range of your measurement
• $t_{\text{min}}$ denote the minimum of the range of your desired target scaling
• $t_{\text{max}}$ denote the maximum of the range of your desired target scaling
• $m\in[r_{\text{min}},r_{\text{max}}]$ denote your measurement to be scaled

Then

$$m\mapsto \frac{m-r_{\text{min}}}{r_{\text{max}}-r_{\text{min}}}\times (t_{\text{max}}-t_{\text{min}}) + t_{\text{min}}$$

will scale $m$ linearly into $[t_{\text{min}},t_{\text{max}}]$ as desired.

To go step by step,

1. $m\mapsto m-r_{\text{min}}$ maps $m$ to $[0,r_{\text{max}}-r_{\text{min}}]$.
2. Next, $$m\mapsto \frac{m-r_{\text{min}}}{r_{\text{max}}-r_{\text{min}}}$$

maps $m$ to the interval $[0,1]$, with $m=r_{\text{min}}$ mapped to $0$ and $m=r_{\text{max}}$ mapped to $1$.

3. Multiplying this by $(t_{\text{max}}-t_{\text{min}})$ maps $m$ to $[0,t_{\text{max}}-t_{\text{min}}]$.

4. Finally, adding $t_{\text{min}}$ shifts everything and maps $m$ to $[t_{\text{min}},t_{\text{max}}]$ as desired.

• Great explanation! I'm in a scenario, where I want to minimize $m$ and want $m$ to take the value of $t_\text{min}$ if $m=r_\text{max}$ and to take the value of $t_\text{max}$ if $m=r_\text{min}$. Thanks to your explanation, this was easy to achieve: Simply swap the numerator to $r_\text{min} - m$ and add $t_\text{max}$ instead of $t_\text{min}$ in the end: $$m\mapsto \frac{r_{\text{min}}-m}{r_{\text{max}}-r_{\text{min}}}\times (t_{\text{max}}-t_{\text{min}}) + t_{\text{max}}$$ – CGFoX Feb 13 '20 at 8:19

In general, to scale your variable $x$ into a range $[a,b]$ you can use: $$x_{normalized} = (b-a)\frac{x - min(x)}{max(x) - min(x)} + a$$