Algorithm to map an interval to a smaller interval

前端 未结 6 2049
生来不讨喜
生来不讨喜 2021-02-02 11:50

I tried searching, but due to the nature of my question, I was unable to find something satisfactory.

My problem is the following: I am trying to map numbers ranging fro

相关标签:
6条回答
  • 2021-02-02 12:24
    // Given a value from intervalA, returns a mapped value from intervalB.
    function intervalicValueMap(intervalA, intervalB, valueIntervalA) {
        var valueIntervalB = (valueIntervalA - intervalA[0]) * (intervalB[1] - intervalB[0]) 
                                / (intervalA[1] - intervalA[0]) + intervalB[0];
    
        valueIntervalB = Math.round(valueIntervalB); // Ommit rounding if not needed.
        return valueIntervalB;
    }
    
    var intervalA = [100, 200];
    var intervalB = [1, 10];
    var valueIntervalA = 170;
    var valueIntervalB = intervalicValueMap(intervalA, intervalB, valueIntervalA);
    
    console.log(valueIntervalB); // Logs 7
    
    0 讨论(0)
  • 2021-02-02 12:30

    I think that instead of giving you a formula of direct mapping, a better approach would be to explain the idea behind it:

    Suppose we want to map an interval [0,1] to interval [1,3], which can be seen as the problem of finding f(x) = Ax + B such that giving any x from interval [0,1], will result in f(x) being/resulting in interval [1,3].

    From this perspective, we already know some values:

    1. x = 0 & f(0) = 1 => f(0) = A*0 + B = 1 => B = 1
    2. x = 1 & f(1) = 3 => f(1) = A*1 + B = 3 <=> A + 1 = 3 => A=2

    From (1) and (2), we may conclude that the function that maps interval [0,1] to [1,3] is f(x) = 2x + 1.

    In you very case, you now should have all necessary knowledge to be able to map [0,2000] interval to [10,100].

    0 讨论(0)
  • 2021-02-02 12:43

    An answer using Numpy from Python:

    import numpy as np  
    
    # [A, B]: old interval
    # [a, b] new interval
    new_value = np.interp(old_value, [A, B], [a, b])
    print(new_value)
    
    0 讨论(0)
  • 2021-02-02 12:47

    A simple linear mapping would map x to x*90/2000+10.

    0 讨论(0)
  • 2021-02-02 12:47

    Here could be an optimized way to map your x data, This pseudo code shows you the main idea for a map function that:

    • Avoid problems with x values out of b1 - b2's range.
    • Deals with array mapping

      function map(var x, var b1, var b2, var s1, var s2)
      {
          var i;
          var result;
      
          i = 0;
          while(i < sizeof(s2))
              if(x < b1)
                  result[i++] = s1;
              else if (x > b2)
                  result[i++] = s2;
              else
                  result[i] = (x - b1) / (b2 - b1 ) * (s2[i] - s1[i]) + s1[i++];
          return (result);
      }
      
    0 讨论(0)
  • 2021-02-02 12:48
    To map
    [A, B] --> [a, b]
    
    use this formula
    (val - A)*(b-a)/(B-A) + a
    

    as correctly mentioned in the other answer it's linear mapping.

    Basically

    y = m*x + c
    
    c = intersection at y-axis
    m = slope determined by two known point (A, a), (B, b) = (b-a)/(B-A)
    
    0 讨论(0)
提交回复
热议问题