58
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ 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')? $\endgroup$
    – IWS
    Commented May 23, 2017 at 9:07
  • $\begingroup$ yes, That would involve a couple of additional loops but can be found. $\endgroup$
    – Saneesh B
    Commented May 23, 2017 at 9:08
  • $\begingroup$ There are many more duplicates. Here's a search: stats.stackexchange.com/…. $\endgroup$
    – whuber
    Commented May 23, 2017 at 13:39
  • $\begingroup$ There are many duplicates with the WRONG answers..... $\endgroup$ Commented Jun 2, 2021 at 6:08

2 Answers 2

116
$\begingroup$

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.

$\endgroup$
7
  • 1
    $\begingroup$ 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}}$$ $\endgroup$ Commented Feb 13, 2020 at 8:19
  • $\begingroup$ Is there a formal name that describes this equation? $\endgroup$ Commented Jun 2, 2021 at 6:08
  • $\begingroup$ @DouglasGaskell: I have only ever seen it referred to as "we scaled variable $x$ to the interval $[a,b]$", where $r_{\text{min}}$ and $r_{\text{max}}$ are implicitly understood to be the minimum and maximum observed values of $x$. $\endgroup$ Commented Jun 2, 2021 at 10:36
  • $\begingroup$ is it somehow possible to get a new scaled value for two same m values? $\endgroup$
    – kuldeep
    Commented Oct 1, 2021 at 14:32
  • $\begingroup$ @kuldeep: scaling is a function. A function will always map identical inputs to identical outputs. You are probably looking for something else. $\endgroup$ Commented Oct 1, 2021 at 16:36
53
$\begingroup$

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 $$

$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.