Mapping one range of numbers to another range

Place code snippets and demo code here
Post Reply
User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

Mapping one range of numbers to another range

Post by Electroguard »

(from Zim)
Changing ranges, like "0 - 1024" to "0 - 3.3" is fairly straight forward, but when the ranges don't start with 0, its more involved.
cicciocb set me straight on this a while back....
eg.

> inMin to inMax is the range to be measured eg. 6 - 120
> out = converted output
> outMin to outMax is the range of the desired output eg. 1 - 5

out = ((in - inMin) / (inMax - inMin) * (outMax - outMin)) + outMin

eg. if sensor reads 15 out of 120 and you want it in a "1 - 5 equivalent"

out = (in - inMin) / (inMax - inMin) * (outMax - outMin) + outMin

out = (15 - 6) / (120 - 6) * (5 - 1) + 1

out = 1.3
User avatar
cicciocb
Site Admin
Posts: 1900
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: Mapping one range of numbers to another range

Post by cicciocb »

Now this can be done more easily with the function CONVERT.MAP

[Local Link Removed for Guests]
Post Reply