6

Example range 1 (minimum and maximum):

[40 ... 480]

Example numbers from range 1:

[42, 59.4, 78.18, 120.43, 416]

Example range 2:

[10 .. 140]

How can I translate the values of the numbers from range 1 to values within the second range? 42 should be equivalent to something between 10 and 11 in the new range.

I'm using PHP but this is more like a math problem.

I know how to align them to the second range:

$diff = $range[0] - $numbers[0];
foreach($numbers as $i => $number){
  $numbers[$i] = $number + $diff;
}

But that's it :(

Alex
  • 66,732
  • 177
  • 439
  • 641

3 Answers3

13

Do you mean something like this, which scales the values linearly to fit within the new range?

var transformRange = (value, r1, r2) => {
  var scale = (r2.max - r2.min) / (r1.max - r1.min)
  return (value - r1.min) * scale;
}

Sample usage:

transformRange(100, {max: 150, min: 50}, {max: 1, min: 0}); => 0.5
Max Hudson
  • 9,961
  • 14
  • 57
  • 107
Katniss
  • 291
  • 1
  • 5
  • Best answer. Easy to understand. – adev Oct 07 '17 at 05:58
  • 4
    It seems that the output range minimum needs to be added to the result? ie: `return (value - r1.min) * scale + r2.min`. See https://jsfiddle.net/afcu4tx0/1/ – wlf May 22 '20 at 10:12
5

You can transform one of the ranges into the other with this function (python)

def transfrom(x):
    return (x - 40)*(130/440.0) + 10

In general the idea is that you want to rebase the ranges (make sure that they both start at zero) and then find how you need to stretch the first range to obtain the second range. So the steps would be

  1. Convert the first range to [0-440] by subtracting 40 and the second range to [0-130] by subtracting 10. This is done to get intervals which start at zero which are easy to scale.
  2. To convert any value from [0-440] to a corresponding value from [0-130] you need to multiply it with 130/440. You can imagine this as shrinking the first interval to a [0-1] interval by dividing with 440 and then stretching it to a [0-130] interval by multiplying.
  3. Now you know how to go from [0-440] to [0-130] and that means that to go from [40-440] to [10-140] you first need to rebase by subtracting 40 multiply by 130/440 and then add 10

Example:

>>> transform(40)
10.0
>>> transform(42)
10.590909090909092
>>> transform(220)
63.181818181818187
>>> transform(480)
140.0
cyon
  • 9,340
  • 4
  • 22
  • 26
1

The correct solution (in PHP) that worked in my case:

function translations_Range2Range($x, $from_min, $from_max, $to_min, $to_max ) {
    if ($from_max == $from_min)
        return $to_max;
    return ($x - $from_min) * ($to_max - $to_min) / ($from_max - $from_min) + $to_min;
}

$x = the value to be translated

$from_min, $from_max = range from

$to_min, $to_max = range to

If there is a case where division by zero occurs, i.e. ($from_max == $from_min) then the function returns $to_max value.

ByREV
  • 31
  • 3