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
8 là 2. Chỉ số thứ hai là 2 - 1, là 1npm i moving-median
9 => độ dài = 6, chúng ta cần chỉ số [2,3]npm i moving-median
- Để có được các chỉ số này, chúng tôi lấy
8 là 3. Chỉ số thứ hai là 3 - 1, là 2npm i moving-median
1 => độ dài = 8, chúng ta cần chỉ số [3,4]2 1.5 2 3.5 2 2 2
- Để có được các chỉ số này, chúng tôi lấy
8 là 4. Chỉ số thứ hai là 4 - 1, là 3npm i moving-median
Khi chúng tôi nhận được các chỉ số [
3,2 1.5 2 3.5 2 2 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, 20] 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, 21Mả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
8, là 1. 5. Chúng tôinpm i moving-median
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
- Để có được chỉ số này, chúng tôi lấy
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
8 là 2. 5. Chúng tôinpm i moving-median
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
- Để có được chỉ số này, chúng tôi lấy
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
8, tức là 3. 5. Chúng tôinpm i moving-median
60 để lấy chỉ số 3npm i moving-median
- Để có được chỉ số này, chúng tôi lấy
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
62logic lặpLặ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