简体   繁体   English

将任何数字数组转换为最大值为 100 的比例值数组

[英]Convert any array of numbers to an array of proportional values with a max value of 100

If I were to receive an array of numbers, how do I convert the numbers proportionally to the max value 100?如果我要接收一个数字数组,如何将这些数字按比例转换为最大值 100? For example, the acceptable range of numbers output must be between 0-100例如,可接受的数字范围 output 必须在 0-100 之间

Sorry in advance for any confusion, I am struggling to phrase the question.如有任何混淆,请提前抱歉,我正在努力表达这个问题。

Here are some examples to show what I mean:这里有一些例子来说明我的意思:

Input Array : [7000, 500, 0, 850]输入数组:[7000, 500, 0, 850]

Output Array : [70, 5, 0, 8.5] Output 阵列:[70, 5, 0, 8.5]

and

Input Array : [70, 550, 0, 1000]输入数组:[70, 550, 0, 1000]

Output Array : [7, 55, 0, 100] Output 数组:[7, 55, 0, 100]

and

Input Array : [7, 5, 0, 8.5]输入数组:[7, 5, 0, 8.5]

Output Array : [70, 50, 0, 85] Output 数组:[70, 50, 0, 85]

To clarify: the output will only be 100 when the max value of the input array is a power of 10.澄清一下:当输入数组的最大值是 10 的幂时,output 只会是 100。

The term is called "normalization".该术语称为“标准化”。 I'll write in standard mathematics terminology that you can Google.我会用标准数学术语写,你可以用谷歌搜索。

var input = [7000, 500, 0, 850]
var domainMin = Math.min.apply(Math, input)
var domainMax = Math.max.apply(Math, input)
var rangeMin = 0
var rangeMax = 100
var output = input.map(n => {
  var basis = (n - domainMin) / (domainMax - domainMin);
  return rangeMin + (rangeMax - rangeMin) * basis;
});

console.log(output);
// outputs [100, 7.142857142857143, 0, 12.142857142857142]

Now it sounds like you want to dynamically pick a different output range.现在听起来您想动态选择不同的 output 范围。

Probably, like:大概,比如:

var input = [7000, 500, 0, 850]
var domainMin = Math.min.apply(Math, input)
var domainMax = Math.max.apply(Math, input)
var rangeMin = domainMin && domainMin / Math.pow(10, Math.floor(Math.log10(domainMin)) - 1)
var rangeMax = domainMax / Math.pow(10, Math.floor(Math.log10(domainMax)) - 1)
console.log(rangeMin, rangeMax)
var output = input.map(n => {
  var basis = (n - domainMin) / (domainMax - domainMin);
  return rangeMin + (rangeMax - rangeMin) * basis;
});
// outputs [70, 5, 0, 8.5]

something like that?类似的东西?

 function convert2Range100 (arr) { let factor = 1, max = Math.max(...arr) if (max <= 10) factor = 0.1 while (max > 100) { factor *= 10 max /= 10 } return arr.map(v=>v/factor) } console.log(JSON.stringify( convert2Range100( [7000, 500, 0, 850]) )) console.log(JSON.stringify( convert2Range100( [70, 550, 0, 1000]) )) console.log(JSON.stringify( convert2Range100( [7, 5, 0, 8.5]) ))

Perhaps something as simple as this?也许像这样简单?

 const scaleTo100 = (xs) => { const factor = 100 / 10 ** (Math.ceil (Math.log10 (Math.max (...xs)))) return xs.map (x => x * factor) } console.log (scaleTo100 ([7000, 500, 0, 850])) console.log (scaleTo100 ([70, 550, 0, 1000])) console.log (scaleTo100 ([7, 5, 0, 8.5]))
 .as-console-wrapper {max-height: 100%;important: top: 0}

Basically we create a factor that tells us what to multiply every input value to be finding the smallest power of 10 at least as large as every number, and then just multiply them.基本上,我们创建一个factor ,告诉我们将每个输入值相乘以找到至少与每个数字一样大的 10 的最小幂,然后将它们相乘。 The factor is calculated by finding the next highest power of 10 from the maximum of our input values and dividing that into 100.该因子是通过从我们的输入值的最大值中找到下一个最高的 10 的幂并将其除以 100 来计算的。

Note that this will fail if all values are zero, if that's a concern.请注意,如果所有值都为零,这将失败,如果这是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM