如何找到两个阵列的平均值?

时间:2022-02-24 12:07:16

I have two arrays which have same entry count, for example:

我有两个具有相同条目数的数组,例如:

var one = [0,20,30,40] ;
var two = [43,20,10,50] ;

I need to find similarity of these two array in percentage,
I tried something like that:

我需要找到这两个数组的百分比的相似性,我尝试过这样的事情:

var avg = (one[0]-two[0] + one[1]-two[1] + one[2]-two[2] + one[3]-two[3]) / 4 ;

But results are not good,
How to calculate a proper average like this?

但结果并不好,如何计算这样的合适平均值?

Edit: Please think of it as like similarity of two sounds data!
Which means:

编辑:请把它想象成两个声音数据的相似性!意思是:

var one = [0,20,30,40] ;
var two = [0,40,60,80] ;

The average of those would be 100%..

平均值为100%..

2 个解决方案

#1


3  

If you want just one number that's the average of both arrays combined, try this:

如果您只想要一个数字,即两个数组的平均值,请尝试以下方法:

var one = [0,20,30,40];
var two = [43,20,10,50];
var i, j;
var average = 0;

for(i = 0; i < one.length; i++){
    average += one[i];
}
for(j = 0; j < two.length; j++){ // (two arrays for flexibility.)
    average += two[j];
}
console.log(average /= (i+j));
// 26.625

If you want the average of the arrays, at the specific indexes, try this:

如果您想要特定索引处的数组的平均值,请尝试以下操作:

var one = [0,20,30,40];
var two = [43,20,10,50];
var i;
var average = [];

for(i = 0; i < one.length; i++){
    average.push((one[i] + two[i]) / 2);
}

console.log(average);
// [21.5, 20, 20, 45]

Edit:

From what I can see of your edit, you want the average percentage that's added to one, to get two. This should work:

从我所看到的你的编辑,你想要添加到一个的平均百分比,得到两个。这应该工作:

var i, n = 0;
var average = 0;

var one = [0,20,30,40] ;
var two = [0,60,70,120] ;

for(i = 0; i < one.length; i++){
    if(one[i] != 0){
        average += two[i] / one[i];
        n++;
    }
}
average = (average / n - 1)*100;

#2


-1  

var one = [0,20,30,40];
var two = [43,20,10,50];
var temp=one.concat(two)
var sum=0 , average=0;
for (i=0; i<temp.length;i++){
sum+=temp[i];
} 
avg=sum/i
document.write(sum);
document.write("<br>");
document.write(avg);

This is a simple code.let me know whether this is what you ask for.

这是一个简单的code.let我知道这是否是你要求的。

#1


3  

If you want just one number that's the average of both arrays combined, try this:

如果您只想要一个数字,即两个数组的平均值,请尝试以下方法:

var one = [0,20,30,40];
var two = [43,20,10,50];
var i, j;
var average = 0;

for(i = 0; i < one.length; i++){
    average += one[i];
}
for(j = 0; j < two.length; j++){ // (two arrays for flexibility.)
    average += two[j];
}
console.log(average /= (i+j));
// 26.625

If you want the average of the arrays, at the specific indexes, try this:

如果您想要特定索引处的数组的平均值,请尝试以下操作:

var one = [0,20,30,40];
var two = [43,20,10,50];
var i;
var average = [];

for(i = 0; i < one.length; i++){
    average.push((one[i] + two[i]) / 2);
}

console.log(average);
// [21.5, 20, 20, 45]

Edit:

From what I can see of your edit, you want the average percentage that's added to one, to get two. This should work:

从我所看到的你的编辑,你想要添加到一个的平均百分比,得到两个。这应该工作:

var i, n = 0;
var average = 0;

var one = [0,20,30,40] ;
var two = [0,60,70,120] ;

for(i = 0; i < one.length; i++){
    if(one[i] != 0){
        average += two[i] / one[i];
        n++;
    }
}
average = (average / n - 1)*100;

#2


-1  

var one = [0,20,30,40];
var two = [43,20,10,50];
var temp=one.concat(two)
var sum=0 , average=0;
for (i=0; i<temp.length;i++){
sum+=temp[i];
} 
avg=sum/i
document.write(sum);
document.write("<br>");
document.write(avg);

This is a simple code.let me know whether this is what you ask for.

这是一个简单的code.let我知道这是否是你要求的。