# Take set of values and change scale [duplicate]

This question already has an answer here:

I have a large array of variable-integer keypairs. The integer values range from -5 to 5. I'd like to scale that data to a range of 0 to 2. Logically, -5 would become 0, 0 would become 1 and 5 would become 2. How should I go about doing this on a large scale and with different input ranges?

-

## marked as duplicate by Rahul, Stefan Hansen, Alex Wertheim, Start wearing purple, Dennis GulkoJun 7 '13 at 7:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

## 2 Answers

If $x$ ranges from $a$ to $b$, then $x-a$ ranges from $0$ to $b-a$, so $(x-a)/(b-a)$ ranges from $0$ to $1$, so $(d-c)(x-a)/(b-a)$ ranges from $0$ to $d-c$, so $${(d-c)(x-a)\over b-a}+c$$ ranges from $c$ to $d$.

-
So in my case, assuming a to be -5 and b to be 5, then wouldn't x's range be 0 to b - a, which would equal 10? – Peter Kazazes Jun 17 '12 at 1:56
Work through what I wrote: $x$ ranges from -5 to 5, $x+5$ ranges from 0 to 10, $(x+5)/(5-(-5))$ ranges from 0 to 1, $(2-0)(x+5)/10$ ranges from 0 to 2, voila! – Gerry Myerson Jun 17 '12 at 2:00

Scale linearly. It is easy to find slope, y-intercept and a little formula.

-