views:

116

answers:

1

hi, for example i have two ranges

                       (1) 0 to 3
                       (2) 10 to 15

in range (1) i have numbers between 0 and 3, where 0 is minimum and 3 is maximum value...(it has also values 1 and 2)...

now i wanted to rescale both ranges (1) and (2) to range 0 to 1. Can you show me how to do it or at least point to helpful sites? thanks a lot!

+2  A: 

What you're describing is called linear interpolation.

For the general case, suppose you have a value c between a and b, and you want a value x between 0 and 1 which is based on c's relative position between a and b. The equation for x is as follows:

x := (c - a) / (b - a)

So if you have a value between 10 and 15 (let's say 11), and you want a value between 0 and 1, you punch the values into the equation above:

x := (11 - 10) / (15 - 10)
x := 1/5

In other words, 11 is one-fifth of the way from 10 to 15.


The even more general case (when you have a value c between a and b and you want a value x between y and z), x is calculated as follows:

x := (c - a) * (z - y) / (b - a) + y

In your case, z = 1 and y = 0.

Welbog