问题描述
我有多个大小为262144的数组,并且尝试使用以下代码将每个数组的所有值设置为0到1(含0和1)之间.需要明确的是,每个数组的归一化方式都不同,因为每个数组都有不同的值.
I have multiple arrays of size 262144, and I am trying to use the following code to set all the values of each array to between 0 and 1, inclusive. To be clear, each array will be normalized differently because each has different values.
var i;
var max = Number.MIN_VALUE;
var min = Number.MAX_VALUE;
for (i = 0; i < array.length; i++) {
if(array[i]>max) {
max = array[i];
}
}
for (i = 0; i < array.length; i++){
if(array[i]<min) {
min = array[i];
}
}
for (i = 0; i < array.length; i++) {
var norm = (array[i]-min)/(max-min);
array[i] = norm;
}
但是,我知道它没有正确执行此操作,因为当我执行以下代码时,登录到控制台的数字通常大于1.
However, I know that it's not doing this correctly because when I do the following code, the numbers logged to the console are often above 1.
max = Number.MIN_VALUE;
min = Number.MAX_VALUE;
for (i = 0; i < array.length; i++) {
if(array[i]>max) {
max = array[i];
}
}
for (i = 0; i < array.length; i++) {
if(array[i]<min) {
min = array[i];
}
}
console.log(max);
console.log(min);
我做错了什么?谢谢!
推荐答案
由于此答案,我找到了解决方案!只是比较数字,因为我没有将它们从字符串转换为浮点数.
I found the solution thanks to this answer! It's simply that the numbers were being compared as I had not converted them from strings to floats.
这篇关于使用Javascript规范化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!