gpt4 book ai didi

javascript - 为什么这种缩放不起作用?

转载 作者:行者123 更新时间:2023-12-01 02:51:01 24 4
gpt4 key购买 nike

目前我正在尝试将数据缩放到适合两个数字之间的大小。我的代码如下:

function dataScaling(db,rangeMax,rangeMin,value) {

var min = db[1].FIELD1;
var max = min;

for (var i=2; i < db.length; i++) {
if (db[i].FIELD1 < min) {min = db[i].FIELD1;}
if (db[i].FIELD1 > max) {max = db[i].FIELD1;}
}

var percent = (value - min) / (max - min);
var answer = percent * (rangeMax - rangeMin) + rangeMin;
return answer;

}

现在当我运行时:

console.log("Min = "+min);
console.log("Maz = "+max);

if (answer > rangeMax) {console.log("Value is Too BIG: "+answer );},我得到:

Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:171 Value is Too BIG: 57.38461538461539
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:171 Value is Too BIG: 49.07692307692308
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:171 Value is Too BIG: 29.692307692307693
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000

第一次和第二次,我得到:

index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:171 Value is Too BIG: 139.14285714285714
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:171 Value is Too BIG: 96.28571428571429
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:171 Value is Too BIG: 87.71428571428571
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000

必须注意的是,每次运行程序时,dataScaling 函数 (db) 的输入都会不断变化。我不明白的是,数据超出了范围。我使用这个调用该函数:

dataScaling(initialArray,20,2,d.FIELD1);

最佳答案

我不确定您在寻找什么,但我认为这是您的解决方案。

/** 
* Re-maps a number from one range to another.
* @param {number} value Value to be converted.
* @param {number} min1 Lower bound of the value's current range.
* @param {number} max1 Upper bound of the value's current range.
* @param {number} min2 Lower bound of the value's target range.
* @param {number} max2 Upper bound of the value's target range.
* @returns {number}
*/
function Map(value, min1, max1, min2, max2) {
return ((value - min1) / (max1 - min1)) * (max2 - min2) + min2;
}

// Lets say this is percentiles
let foo = 25;

// You want the percentiles to a scale from 0 to 2
let doo = Map(foo, 0, 100, 0, 2)
console.log(doo);

// From the scale 0-2 back to percentiles.
console.log(Map(doo, 0, 2, 0, 100));

关于javascript - 为什么这种缩放不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46973488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com