# Transforming range of variable to 0 to 1 and custom ranges

Created:18 Jul 2020 00:39:07 , in Maths

One useful skill when it comes to dealing with variables is to be able to change their range. One of the most useful and desirable ranges is [0,1]. Occasionally you might need a different range like [-1,1], too. This article describes a couple of methods of scaling and descaling a variable's range.

## Quick and easy transformation to [0,1]

One way to scale range of variable x to range from 0 to 1 is based on the following function:

scaled variable x, called y, will be in the range between 0 and 1.

Scaling a to custom range

If a range from a to b, where a is the minimum and b is the maximum allowed value, is required, one of the three following formulas can be used:

**1**)

**2**)

where e is the Euler's number

**3**)

## Scaling variable from [0,1] to a custom range

If a variable y is in range from 0 to 1, it can be scaled to a variable z which is in range between a and b as follows:

## Min-max scaling

Min-max scaling, also known as min-max normalization, is another easy way, which is frequently used for transforming variable's range.

The formula for min-max scaling is as follows:

where a and b denote minimum and maximum values of x in a set of data respectively.

Descaling variable y back to x can be done using:

## Min-max scaling in PHP

Two functions given below perform min-max scaling and descaling as described in the previous section.

```
function minMaxNormalize($x, $a, $b) {
$y = false;
try {
$y = ($x - $a) / ($b - $a);
}catch(DivisionByZeroError $e){
echo $e -> getMessage(),"\n";
}
return $y;
}
function minMaxDenormalize($y, $a, $b) {
$x = ($y * ($b - $a) + $a);
return $x;
}
```

Here is how to use these functions

Suppose that the range of values of x is from -10 to 10, but we want it to be between 0 and 1. To transform the range, do the following:

```
minMaxNormalize(7, -10, 10);
// => 0.85
```

To transform variables to be back in the range [-10,10] :

```
minMaxDenormalize(0.85,-10,10);
// => 7
```

## Conclusion

As can be seen in this article, there are quiet a few ways of transforming a range of a variable both to [0,1] or to a custom range. I hope you have found at least one of them sufficient for your purposes. If you have something to add, you are welcome to post a comment below.

This post was updated on 18 Jul 2020 01:30:45

**Tags: **
php

## Author, Copyright and citation

### Author

Author of the above article, **Sylwester Wojnowski**, enjoys sWWW writing computer code in PHP, JavaScript and BASH, and some other things he wrote more on on the About page of this website.

### Copyrights

©Copyright, 2020 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

### Citation

Cite this article as:

Wojnowski, Sylwester. "Transforming range of variable to 0 to 1 and custom ranges." From sWWW - Code For The Web . https://wojnowski.net.pl//main/index/transforming-range-of-variable-to-0-to-1-and-custom-ranges

## Add Comment