从 [0.5 - 1] 标准化为 [0 - 1]

发布于 2024-08-05 05:20:32 字数 112 浏览 6 评论 0原文

我有点被困在这里,我想这有点像一个脑筋急转弯。如果我的数字在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

〆一缕阳光ご 2024-08-12 05:20:32

其他人为你提供了公式,但没有提供工作。以下是解决此类问题的方法。您可能会发现这比仅仅知道答案更有价值。

为了将 [0.5, 1] 映射到 [0, 1],我们将寻找 x -> 形式的线性映射。斧头+b。我们将要求端点映射到端点并保留该顺序。

方法一:端点映射到端点并保留顺序的要求意味着 0.5 映射到 01< /code> 映射到 1

a * (0.5) + b = 0 (1)
a * 1 + b = 1     (2)

这是一个线性方程联立系统,可以通过将方程 (1) 乘以 -2 来求解,将方程 (1) 添加到方程 (2) 中。这样做后,我们得到 b = -1 并将其代入方程 (2),我们得到 a = 2。因此,地图 x -> 2x - 1 就可以了。

方法二:通过两点(x1, y1)(x2, y2)的直线的斜率是

(y2 - y1) / (x2 - x1).

这里我们将使用点 (0.5, 0)(1, 1) 以满足端点映射到端点以及映射保序的要求。因此斜率是

m = (1 - 0) / (1 - 0.5) = 1 / 0.5 = 2.

我们有 (1, 1) 是线上的一个点,因此通过直线方程的点斜率形式,我们可以得到这样的结果,

y - 1 = 2 * (x - 1) = 2x - 2

因此

y = 2x - 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 form x -> 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 to 0 and 1 is mapped to 1

a * (0.5) + b = 0 (1)
a * 1 + b = 1     (2)

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 obtain b = -1 and substituting this back into equation (2) we obtain that a = 2. Thus the map x -> 2x - 1 will do the trick.

Method two: The slope of a line passing through two points (x1, y1) and (x2, y2) is

(y2 - y1) / (x2 - x1).

Here 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 is

m = (1 - 0) / (1 - 0.5) = 1 / 0.5 = 2.

We 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 that

y - 1 = 2 * (x - 1) = 2x - 2

so that

y = 2x - 1.

Once again we see that x -> 2x - 1 is a map that will do the trick.

記柔刀 2024-08-12 05:20:32

减去 0.5(给出新的 0 - 0.5 范围),然后乘以 2。

double normalize( double x )
{
    // I'll leave range validation up to you
    return (x - 0.5) * 2;
}

Subtract 0.5 (giving you a new range of 0 - 0.5) then multiply by 2.

double normalize( double x )
{
    // I'll leave range validation up to you
    return (x - 0.5) * 2;
}
瑕疵 2024-08-12 05:20:32

添加另一个通用答案。

如果要将线性范围 [A..B] 映射到 [C..D],可以应用以下步骤:

移动范围,使下限为 0。(从两个边界中减去 A:

[A..B] -> [0..B-A]

缩放范围,使它是 [0..1]。(除以上限):

[0..B-A] -> [0..1]

缩放范围,使其具有新范围的长度,即 DC(乘以 DC):

[0..1] ->  [0..D-C]

移动范围,使下限为 C。(将 C 添加到边界):

[0..D-C] -> [C..D]

将其合并为一个公式,我们得到:

       (D-C)*(X-A)
X' =   -----------  + C
          (B-A)

在您的情况下,A=0.5,B=1,C=0,D=1 您得到:

       (X-0.5)
X' =   ------- = 2X-1
        (0.5)

注意,如果您必须转换大量 X对于 X',您可以将公式更改为:

       (D-C)         C*B - A*D
X' =   ----- * X  +  ---------  
       (B-A)           (B-A)

看一下非线性范围也很有趣 您可以采取相同的步骤,但需要额外的步骤将线性范围转换为非线性范围。

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:

[A..B] -> [0..B-A]

Scale the range so it is [0..1]. (divide by the upper bound):

[0..B-A] -> [0..1]

Scale the range so it has the length of the new range which is D-C. (multiply with D-C):

[0..1] ->  [0..D-C]

Shift the range so the lower bound is C. (add C to the bounds):

[0..D-C] -> [C..D]

Combining this to a single formula, we get:

       (D-C)*(X-A)
X' =   -----------  + C
          (B-A)

In your case, A=0.5, B=1, C=0, D=1 you get:

       (X-0.5)
X' =   ------- = 2X-1
        (0.5)

Note, if you have to convert a lot of X to X', you can change the formula to:

       (D-C)         C*B - A*D
X' =   ----- * X  +  ---------  
       (B-A)           (B-A)

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.

攀登最高峰 2024-08-12 05:20:32

Lazyweb 答案: 将值 x[minimum..maximum] 转换为 [floor..ceil]

一般情况:

normalized_x = ((ceil - floor) * (x - minimum))/(maximum - minimum) + floor

标准化为 [0..255]:

normalized_x = (255 * (x - minimum))/(maximum - minimum)

标准化为 [0..1]:

normalized_x = (x - minimum)/(maximum - minimum)

Lazyweb answer: To convert a value x from [minimum..maximum] to [floor..ceil]:

General case:

normalized_x = ((ceil - floor) * (x - minimum))/(maximum - minimum) + floor

To normalize to [0..255]:

normalized_x = (255 * (x - minimum))/(maximum - minimum)

To normalize to [0..1]:

normalized_x = (x - minimum)/(maximum - minimum)
情话已封尘 2024-08-12 05:20:32

× 2 − 1

应该可以解决问题

× 2 − 1

should do the trick

神经大条 2024-08-12 05:20:32

您始终可以在数学中使用钳位或饱和来确保您的最终值在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文