Di chuyển trung bình coderbyte javascript

di chuyển trung bình

Tính toán trung bình đang chạy từ một luồng giá trị. Hữu ích để lọc tín hiệu nhiễu

Ví dụ

var createMedianFilter = require['moving-median']

var median = createMedianFilter[5]

 

var sequence = [1, 2, 5, 2, 1, 1, 1, 2, 1000, 2, NaN, 3, NaN, 3, 3]

 

for[var i=0; i độ dài = 4, chúng ta cần chỉ số [1,2]

  • Để có được các chỉ số này, chúng tôi lấy
    npm i moving-median
    
    8 là 2. Chỉ số thứ hai là 2 - 1, là 1
  • npm i moving-median
    
    9 => độ dài = 6, chúng ta cần chỉ số [2,3]
    • Để có được các chỉ số này, chúng tôi lấy
      npm i moving-median
      
      8 là 3. Chỉ số thứ hai là 3 - 1, là 2
  • 2
    1.5
    2
    3.5
    2
    2
    2
    
    1 => độ dài = 8, chúng ta cần chỉ số [3,4]
    • Để có được các chỉ số này, chúng tôi lấy
      npm i moving-median
      
      8 là 4. Chỉ số thứ hai là 4 - 1, là 3
  • Khi chúng tôi nhận được các chỉ số [

    2
    1.5
    2
    3.5
    2
    2
    2
    
    3,
    const ary = [2, 1, 5, 7, 2, 0, 5]
    
    
    function calculateMedian[ary] {
      const sorted = ary.sort[[a, b] => a - b]
    
      if [sorted.length == 0] return 0
    
      if [sorted.length == 1] return sorted[0]
    
      if [sorted.length % 2 == 0] {
        const firstIndex = sorted.length / 2
        const secondIndex = firstIndex - 1
        return [[sorted[firstIndex] + sorted[secondIndex]] / 2]
      } else {
        const middleIndex = Math.floor[sorted.length / 2]
        return [sorted[middleIndex]]
      }
    }
    
    function iterateArray[ary] {
      ary.forEach[[el, index, ar] => {
        const currentStreamedArray = ar.slice[0, index + 1]
        console.log[calculateMedian[currentStreamedArray]]  //2, 1.5, 2, 3.5, 2, 2, 2
      }]
    }
    
    
    iterateArray[ary] //2, 1.5, 2, 3.5, 2, 2, 2
    0] cho chẵn, trung bình là
    const ary = [2, 1, 5, 7, 2, 0, 5]
    
    
    function calculateMedian[ary] {
      const sorted = ary.sort[[a, b] => a - b]
    
      if [sorted.length == 0] return 0
    
      if [sorted.length == 1] return sorted[0]
    
      if [sorted.length % 2 == 0] {
        const firstIndex = sorted.length / 2
        const secondIndex = firstIndex - 1
        return [[sorted[firstIndex] + sorted[secondIndex]] / 2]
      } else {
        const middleIndex = Math.floor[sorted.length / 2]
        return [sorted[middleIndex]]
      }
    }
    
    function iterateArray[ary] {
      ary.forEach[[el, index, ar] => {
        const currentStreamedArray = ar.slice[0, index + 1]
        console.log[calculateMedian[currentStreamedArray]]  //2, 1.5, 2, 3.5, 2, 2, 2
      }]
    }
    
    
    iterateArray[ary] //2, 1.5, 2, 3.5, 2, 2, 2
    1

    Mảng độ dài lẻ

    • const ary = [2, 1, 5, 7, 2, 0, 5]
      
      
      function calculateMedian[ary] {
        const sorted = ary.sort[[a, b] => a - b]
      
        if [sorted.length == 0] return 0
      
        if [sorted.length == 1] return sorted[0]
      
        if [sorted.length % 2 == 0] {
          const firstIndex = sorted.length / 2
          const secondIndex = firstIndex - 1
          return [[sorted[firstIndex] + sorted[secondIndex]] / 2]
        } else {
          const middleIndex = Math.floor[sorted.length / 2]
          return [sorted[middleIndex]]
        }
      }
      
      function iterateArray[ary] {
        ary.forEach[[el, index, ar] => {
          const currentStreamedArray = ar.slice[0, index + 1]
          console.log[calculateMedian[currentStreamedArray]]  //2, 1.5, 2, 3.5, 2, 2, 2
        }]
      }
      
      
      iterateArray[ary] //2, 1.5, 2, 3.5, 2, 2, 2
      2 => độ dài = 3, chúng ta cần chỉ số [1]
      • Để có được chỉ số này, chúng tôi lấy
        npm i moving-median
        
        8, là 1. 5. Chúng tôi
        const ary = [2, 1, 5, 7, 2, 0, 5]
        
        
        function calculateMedian[ary] {
          const sorted = ary.sort[[a, b] => a - b]
        
          if [sorted.length == 0] return 0
        
          if [sorted.length == 1] return sorted[0]
        
          if [sorted.length % 2 == 0] {
            const firstIndex = sorted.length / 2
            const secondIndex = firstIndex - 1
            return [[sorted[firstIndex] + sorted[secondIndex]] / 2]
          } else {
            const middleIndex = Math.floor[sorted.length / 2]
            return [sorted[middleIndex]]
          }
        }
        
        function iterateArray[ary] {
          ary.forEach[[el, index, ar] => {
            const currentStreamedArray = ar.slice[0, index + 1]
            console.log[calculateMedian[currentStreamedArray]]  //2, 1.5, 2, 3.5, 2, 2, 2
          }]
        }
        
        
        iterateArray[ary] //2, 1.5, 2, 3.5, 2, 2, 2
        4 để lấy chỉ số 1
    • const ary = [2, 1, 5, 7, 2, 0, 5]
      
      
      function calculateMedian[ary] {
        const sorted = ary.sort[[a, b] => a - b]
      
        if [sorted.length == 0] return 0
      
        if [sorted.length == 1] return sorted[0]
      
        if [sorted.length % 2 == 0] {
          const firstIndex = sorted.length / 2
          const secondIndex = firstIndex - 1
          return [[sorted[firstIndex] + sorted[secondIndex]] / 2]
        } else {
          const middleIndex = Math.floor[sorted.length / 2]
          return [sorted[middleIndex]]
        }
      }
      
      function iterateArray[ary] {
        ary.forEach[[el, index, ar] => {
          const currentStreamedArray = ar.slice[0, index + 1]
          console.log[calculateMedian[currentStreamedArray]]  //2, 1.5, 2, 3.5, 2, 2, 2
        }]
      }
      
      
      iterateArray[ary] //2, 1.5, 2, 3.5, 2, 2, 2
      5 => độ dài = 5, chúng ta cần chỉ số [2]
      • Để có được chỉ số này, chúng tôi lấy
        npm i moving-median
        
        8 là 2. 5. Chúng tôi
        const ary = [2, 1, 5, 7, 2, 0, 5]
        
        
        function calculateMedian[ary] {
          const sorted = ary.sort[[a, b] => a - b]
        
          if [sorted.length == 0] return 0
        
          if [sorted.length == 1] return sorted[0]
        
          if [sorted.length % 2 == 0] {
            const firstIndex = sorted.length / 2
            const secondIndex = firstIndex - 1
            return [[sorted[firstIndex] + sorted[secondIndex]] / 2]
          } else {
            const middleIndex = Math.floor[sorted.length / 2]
            return [sorted[middleIndex]]
          }
        }
        
        function iterateArray[ary] {
          ary.forEach[[el, index, ar] => {
            const currentStreamedArray = ar.slice[0, index + 1]
            console.log[calculateMedian[currentStreamedArray]]  //2, 1.5, 2, 3.5, 2, 2, 2
          }]
        }
        
        
        iterateArray[ary] //2, 1.5, 2, 3.5, 2, 2, 2
        7 để lấy chỉ số 2
    • const ary = [2, 1, 5, 7, 2, 0, 5]
      
      
      function calculateMedian[ary] {
        const sorted = ary.sort[[a, b] => a - b]
      
        if [sorted.length == 0] return 0
      
        if [sorted.length == 1] return sorted[0]
      
        if [sorted.length % 2 == 0] {
          const firstIndex = sorted.length / 2
          const secondIndex = firstIndex - 1
          return [[sorted[firstIndex] + sorted[secondIndex]] / 2]
        } else {
          const middleIndex = Math.floor[sorted.length / 2]
          return [sorted[middleIndex]]
        }
      }
      
      function iterateArray[ary] {
        ary.forEach[[el, index, ar] => {
          const currentStreamedArray = ar.slice[0, index + 1]
          console.log[calculateMedian[currentStreamedArray]]  //2, 1.5, 2, 3.5, 2, 2, 2
        }]
      }
      
      
      iterateArray[ary] //2, 1.5, 2, 3.5, 2, 2, 2
      8 => độ dài = 7, chúng ta cần chỉ số [3]
      • Để có được chỉ số này, chúng tôi lấy
        npm i moving-median
        
        8, tức là 3. 5. Chúng tôi
        npm i moving-median
        
        60 để lấy chỉ số 3

    Khi chúng tôi nhận được các chỉ số [

    npm i moving-median
    
    61] cho tỷ lệ cược, trung bình chỉ là
    npm i moving-median
    
    62

    logic lặp

    Lặp qua mảng. Trong mỗi lần lặp, chúng tôi xuất một mảng mới từ đầu đến chỉ mục trình lặp hiện tại. Sau đó gửi mảng mới đó qua

    npm i moving-median
    
    63

    Chủ Đề