带负数的值的缩放范围 [英] Scaling range of values with negative numbers
问题描述
如果它们包含负数,如何缩放一组值以适应新的范围?
How can I scale a set of values to fit a new range if they include negative numbers?
例如,我有一组数字(-10,-9、1、4、10),这些数字必须缩放到[0 1]范围,例如-10映射到0,10映射到1
For example, I have a set of numbers (-10, -9, 1, 4, 10) which have to scaled to a range [0 1], such that -10 maps to 0, and 10 maps to 1.
任意数字"x"的常规方法是: (x-from_min)*(to_max-to_min)/(from_max-from_min)+ to_min
The regular method for an arbitrary number 'x' would be: (x - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
,但不适用于负数.任何帮助表示赞赏.谢谢!
but this does not work for negative numbers. Any help is appreciated. Thanks!!
推荐答案
我相信id可以;在您的示例中,
I believe id does; in your example,
from_min = -10,
from_max = 10,
to_max = 1,
to_min = 0.
这产生
to_max - to_min = 1,
from_max - from_min = 20;
所以使用公式
x -> (x - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
= (x - from_min) * 1 / 20 + 0
= (x - from_min) / 20
收益
-10 -> (-10 + 10) / 20 = 0 / 20,
-9 -> (-9 + 10) / 20 = 1 / 20,
1 -> (1 + 10) / 20 = 11 / 20,
4 -> (4 + 10) / 20 = 14 / 20,
10 -> (10 + 10) / 20 = 20 / 20,
因此所有结果值均为非负值.此外,原始最小值-10映射到to_min = 0,原始最大值10映射到to_max =1.如果这在您的实现中不起作用,请检查是否混合了整数类型和浮点类型.
so all the resulting values are nonnegative. Furthermore, the original minimum -10 maps to to_min = 0 and the original maximum 10 maps to to_max = 1. If this doesn't work in your implementation, check if you mixed up integral types and floating-point types.
这篇关于带负数的值的缩放范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!