Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math - mapping numbers

How do I map numbers, linearly, between a and b to go between c and d.

That is, I want numbers between 2 and 6 to map to numbers between 10 and 20... but I need the generalized case.

My brain is fried.

like image 597
Sam Avatar asked Dec 05 '08 21:12

Sam


People also ask

What are mapping in math?

Mapping applies to any set: a collection of objects, such as all whole numbers, all the points on a line, or all those inside a circle. For example, “multiply by two” defines a mapping of the set of all whole numbers onto the set of even numbers. A rotation is a map of a plane or of all of space into itself.

What are the examples of mapping?

An example of mapping is creating a map to get to your house. An example of mapping is identifying which cell on one spreadsheet contains the same information as the cell on another speadsheet. The process of making maps. (computing) Assigning a PC to a shared drive or printer port on a network.


2 Answers

If your number X falls between A and B, and you would like Y to fall between C and D, you can apply the following linear transform:

Y = (X-A)/(B-A) * (D-C) + C 

That should give you what you want, although your question is a little ambiguous, since you could also map the interval in the reverse direction. Just watch out for division by zero and you should be OK.

like image 104
PeterAllenWebb Avatar answered Sep 20 '22 06:09

PeterAllenWebb


Divide to get the ratio between the sizes of the two ranges, then subtract the starting value of your inital range, multiply by the ratio and add the starting value of your second range. In other words,

R = (20 - 10) / (6 - 2) y = (x - 2) * R + 10 

This evenly spreads the numbers from the first range in the second range.

like image 23
Konrad Rudolph Avatar answered Sep 19 '22 06:09

Konrad Rudolph