Normalizing from [0.5 - 1] to [0 - 1]

前端 未结 6 1684
自闭症患者
自闭症患者 2021-01-30 02:45

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 a

6条回答
  •  半阙折子戏
    2021-01-30 03:15

    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.

提交回复
热议问题