从 [0.5 - 1] 标准化为 [0 - 1]
我有点被困在这里,我想这有点像一个脑筋急转弯。如果我的数字在 0.5 到 1 之间,如何将其标准化为 0 到 1 之间?
感谢您的帮助,也许我只是有点慢,因为我过去 24 小时一直在工作 O_O
I'm kind of stuck here, I guess it's a bit of a brain teaser. If I have numbers in the range between 0.5 to 1 how can I normalize it to be between 0 to 1?
Thanks for any help, maybe I'm just a bit slow since I've been working for the past 24 hours straight O_O
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
其他人为你提供了公式,但没有提供工作。以下是解决此类问题的方法。您可能会发现这比仅仅知道答案更有价值。
为了将
[0.5, 1]
映射到[0, 1]
,我们将寻找x -> 形式的线性映射。斧头+b。我们将要求端点映射到端点并保留该顺序。
方法一:端点映射到端点并保留顺序的要求意味着
0.5
映射到0
和1< /code> 映射到
1
这是一个线性方程联立系统,可以通过将方程
(1)
乘以-2
来求解,将方程(1)
添加到方程(2)
中。这样做后,我们得到b = -1
并将其代入方程(2)
,我们得到a = 2
。因此,地图x -> 2x - 1 就可以了。
方法二:通过两点
(x1, y1)
和(x2, y2)
的直线的斜率是这里我们将使用点
(0.5, 0)
和(1, 1)
以满足端点映射到端点以及映射保序的要求。因此斜率是我们有
(1, 1)
是线上的一个点,因此通过直线方程的点斜率形式,我们可以得到这样的结果,因此
我们再次看到 <代码>x-> 2x - 1 是一张可以实现这一目的的地图。
Others have provided you the formula, but not the work. Here's how you approach a problem like this. You might find this far more valuable than just knowning the answer.
To map
[0.5, 1]
to[0, 1]
we will seek a linear map of the formx -> ax + b
. We will require that endpoints are mapped to endpoints and that order is preserved.Method one: The requirement that endpoints are mapped to endpoints and that order is preserved implies that
0.5
is mapped to0
and1
is mapped to1
This is a simultaneous system of linear equations and can be solved by multiplying equation
(1)
by-2
and adding equation(1)
to equation(2)
. Upon doing this we obtainb = -1
and substituting this back into equation(2)
we obtain thata = 2
. Thus the mapx -> 2x - 1
will do the trick.Method two: The slope of a line passing through two points
(x1, y1)
and(x2, y2)
isHere we will use the points
(0.5, 0)
and(1, 1)
to meet the requirement that endpoints are mapped to endpoints and that the map is order-preserving. Therefore the slope isWe have that
(1, 1)
is a point on the line and therefore by the point-slope form of an equation of a line we have thatso that
Once again we see that
x -> 2x - 1
is a map that will do the trick.减去 0.5(给出新的 0 - 0.5 范围),然后乘以 2。
Subtract 0.5 (giving you a new range of 0 - 0.5) then multiply by 2.
添加另一个通用答案。
如果要将线性范围 [A..B] 映射到 [C..D],可以应用以下步骤:
移动范围,使下限为 0。(从两个边界中减去 A:
缩放范围,使它是 [0..1]。(除以上限):
缩放范围,使其具有新范围的长度,即 DC(乘以 DC):
移动范围,使下限为 C。(将 C 添加到边界):
将其合并为一个公式,我们得到:
在您的情况下,A=0.5,B=1,C=0,D=1 您得到:
注意,如果您必须转换大量 X对于 X',您可以将公式更改为:
看一下非线性范围也很有趣 您可以采取相同的步骤,但需要额外的步骤将线性范围转换为非线性范围。
To add another generic answer.
If you want to map the linear range [A..B] to [C..D], you can apply the following steps:
Shift the range so the lower bound is 0. (subract A from both bounds:
Scale the range so it is [0..1]. (divide by the upper bound):
Scale the range so it has the length of the new range which is D-C. (multiply with D-C):
Shift the range so the lower bound is C. (add C to the bounds):
Combining this to a single formula, we get:
In your case, A=0.5, B=1, C=0, D=1 you get:
Note, if you have to convert a lot of X to X', you can change the formula to:
It is also interesting to take a look at non linear ranges. You can take the same steps, but you need an extra step to transform the linear range to a nonlinear range.
Lazyweb 答案: 将值
x
从[minimum..maximum]
转换为[floor..ceil]
:一般情况:
标准化为 [0..255]:
标准化为 [0..1]:
Lazyweb answer: To convert a value
x
from[minimum..maximum]
to[floor..ceil]
:General case:
To normalize to [0..255]:
To normalize to [0..1]:
应该可以解决问题
should do the trick
您始终可以在数学中使用钳位或饱和来确保您的最终值在 0-1 之间。有些在最后饱和了,但我也看到它在计算过程中完成了。
You could always use clamp or saturate within your math to make sure your final value is between 0-1. Some saturate at the end, but I've seen it done during a computation, too.