A function or a factor to scale a list of real numbers from one range to another

algebra-precalculustransformation

Edit:

I made a mistake or someone did. The a and b and max and min is swapped in the formula I used. Upon switching the a for min and b for max, the algorithm worked as I expected.

Previously I had

a = -180, b= 180, and then min -100, max = 100

instead it should've been

a = -100, b= 100, and then min -180, max = 180

And based on the answer below, I figured out that the x is the series I'm working with.

After that it was just matter of pulling the (x-min) out of the fraction to make it easier to realize what to do next.

$$f(x)=(x – min)\frac{(b-a)}{max – min} + {a}$$

Since x was my series, all I had to do with subtract min (the right number this time), and then the rest was just simple routine.


I posted this in a wrong group, but I think this is more of a simple math problem, which I jut can't wrap my mind around.

Let's say you have 2 lists of real numbers.

list_1 ranges from -100 to 100 over time.

list_2 ranges from -80 to 80 over time. (exact same time as list_1)

And then I have a third list, which is a sum of the list_1 and list_2, (let's call it list_sum) thus can range from -180 to 180.

I would like to come up with a formula to scale the sum of the lists, list_sum, to the min and max of either list_1 or list_2.

So basically, I'm trying to come up with

list_sum * factor 

or

function(list_sum)

that should give me the list_sum's min and max to be within the range of list_1 or list_2.

It's gotta be related to (based on some search i've done)

$$f(x) = \frac{(b-a)(x – min)}{max – min} + {a}$$

How so?

If given the list_sum (gives me min and max of my list) and list_1 (gives me a and b of the scale) to be scaled to,

a = -100, b= 100, and then min -180, max = 180

that gives me

$$\frac{(100-(-100))(x – (-180))}{180- (-180)} + {-100}$$

or

$$\frac{(200)(x + 180)}{360} {-100}$$

Does that seems right to anyone?

if so, how do I use that if my list_sum is to be transformed?

Thanks for the help.

Best Answer

So basically you have a list (list_sum) and you would like to scale it so that the minimum is some fixed number $a$ and the maximum is another fixed number $b$. In other words, you would like a function $f(x)$ that has the properties $$ f(\text{min})=a \quad \text{and}\quad f(\text{max})=b $$ We can make this with a linear function $f(x) = kx+c$. Plugging in the conditions, we get $$ \left\{ \begin{array}{ccc} \text{min}\cdot k &+& c &= a \\ \text{max}\cdot k &+& c &= b\\ \end{array} \right. $$ The values of $k$ and $c$ can be solved for: $$\tag{1} \left\{ \begin{array}{cl} k = & \frac{b-a}{\text{max}-\text{min}} \\ c = & \frac{a\cdot \text{max}-b\cdot \text{min}}{\text{max}-\text{min}} \end{array} \right. $$ Inserting the values and simplifying, we get $$ \tag{2} f(x) = \frac{b(x - \text{min}) + a(\text{max}-x)}{\text{max}-\text{min}} $$ A more computationally efficient way is to first calculate the coefficients from Equation ($1$) and then calculate $f(x) = kx+c$.

Related Question