Scaling Between Two Number Ranges

How to scale down a range of numbers with a known min and max value

Let's say you want to scale a range [min,max] to [a,b]. You're looking for a (continuous) function that satisfies

f(min) = a
f(max) = b

In your case, a would be 1 and b would be 30, but let's start with something simpler and try to map [min,max] into the range [0,1].

Putting min into a function and getting out 0 could be accomplished with

f(x) = x - min   ===>   f(min) = min - min = 0

So that's almost what we want. But putting in max would give us max - min when we actually want 1. So we'll have to scale it:

        x - min                                  max - min
f(x) = --------- ===> f(min) = 0; f(max) = --------- = 1
max - min max - min

which is what we want. So we need to do a translation and a scaling. Now if instead we want to get arbitrary values of a and b, we need something a little more complicated:

       (b-a)(x - min)
f(x) = -------------- + a
max - min

You can verify that putting in min for x now gives a, and putting in max gives b.

You might also notice that (b-a)/(max-min) is a scaling factor between the size of the new range and the size of the original range. So really we are first translating x by -min, scaling it to the correct factor, and then translating it back up to the new minimum value of a.

Scaling between two number ranges

Use percentages:

xMax = 10;
xMin = 1;

yMax = 559.22;
yMin = 300.77;

percent = (inputY - yMin) / (yMax - yMin);
outputX = percent * (xMax - xMin) + xMin;

Convert a number range to another range, maintaining ratio

NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin

Or a little more readable:

OldRange = (OldMax - OldMin)  
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin

Or if you want to protect for the case where the old range is 0 (OldMin = OldMax):

OldRange = (OldMax - OldMin)
if (OldRange == 0)
NewValue = NewMin
else
{
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
}

Note that in this case we're forced to pick one of the possible new range values arbitrarily. Depending on context, sensible choices could be: NewMin (see sample), NewMax or (NewMin + NewMax) / 2

Scaling a number between a certain range to that of another in python

Try this:

def scale(x, srcRange, dstRange):
return (x-srcRange[0])*(dstRange[1]-dstRange[0])/(srcRange[1]-srcRange[0])+dstRange[0]

Of course, each range must be given in the form of (min, max).

In your example, one of the ranges is given the form of (max, min).

Scaling a number between two values

You can't scale a number in a range if you don't know the range.

Maybe what you're looking for is the modulo operator. Modulo is basically the remainder of division, the operator in most languages is is %.

0 % 5 == 0
1 % 5 == 1
2 % 5 == 2
3 % 5 == 3
4 % 5 == 4
5 % 5 == 0
6 % 5 == 1
7 % 5 == 2
...

How to Scale numbers/values

This is actually simple maths

First, let's remove the need of a negative number :

For range -60 <-> + 20 : x + 60

Now we have a 0 <-> 80 range, just scale it to 255 : ( x / 80 ) * 255

Put that all in a formula, and this is what you should get :
y = ( (x + 60 ) / 80 ) * 255

So basically :

y = ( (x + negativeValue ) / MaxValue ) * MaxScale

I hope you understand now !

scale changing number to a range using javascript

The scale factor is (newMax - newMin) / (prevMax - prevMin). You also need to move along the number line by newMin - prevMin

function generateScaleFunction(prevMin, prevMax, newMin, newMax) {
var offset = newMin - prevMin,
scale = (newMax - newMin) / (prevMax - prevMin);
return function (x) {
return offset + scale * x;
};
};

Usage

var fn = generateScaleFunction(0, 100, 20, 60);
fn( 0); // 20
fn( 25); // 30
fn( 50); // 40
fn( 75); // 50
fn(100); // 60

// please note you are not guaranteed an int
fn(1); // 20.4

Scale a series between two points

It's straight-forward to create a small function to do this using basic arithmetic:

s = sort(rexp(100))

range01 <- function(x){(x-min(x))/(max(x)-min(x))}

range01(s)

[1] 0.000000000 0.003338782 0.007572326 0.012192201 0.016055006 0.017161145
[7] 0.019949532 0.023839810 0.024421602 0.027197168 0.029889484 0.033039408
[13] 0.033783376 0.038051265 0.045183382 0.049560233 0.056941611 0.057552543
[19] 0.062674982 0.066001242 0.066420884 0.067689067 0.069247825 0.069432174
[25] 0.070136067 0.076340460 0.078709590 0.080393512 0.085591881 0.087540132
[31] 0.090517295 0.091026499 0.091251213 0.099218526 0.103236344 0.105724733
[37] 0.107495340 0.113332392 0.116103438 0.124050331 0.125596034 0.126599323
[43] 0.127154661 0.133392300 0.134258532 0.138253452 0.141933433 0.146748798
[49] 0.147490227 0.149960293 0.153126478 0.154275371 0.167701855 0.170160948
[55] 0.180313542 0.181834891 0.182554291 0.189188137 0.193807559 0.195903010
[61] 0.208902645 0.211308713 0.232942314 0.236135220 0.251950116 0.260816843
[67] 0.284090255 0.284150541 0.288498370 0.295515143 0.299408623 0.301264703
[73] 0.306817872 0.307853369 0.324882091 0.353241217 0.366800517 0.389474449
[79] 0.398838576 0.404266315 0.408936260 0.409198619 0.415165553 0.433960390
[85] 0.440690262 0.458692639 0.464027428 0.474214070 0.517224262 0.538532221
[91] 0.544911543 0.559945121 0.585390414 0.647030109 0.694095422 0.708385079
[97] 0.736486707 0.787250428 0.870874773 1.000000000

Scale a number by given range

Just calculate

function f(x)  {          // [0 ... 1]
return 5 * x + 1; // [1 ... 6]
}

The other way round

function f(y)  {          // [1 ... 6]
return (y - 1) / 5; // [0 ... 1]
}


Related Topics



Leave a reply



Submit