将多个数组的每一项平均到一个数组中
Posted
技术标签:
【中文标题】将多个数组的每一项平均到一个数组中【英文标题】:Getting average of every item of multiple arrays into one array 【发布时间】:2018-02-08 17:37:54 【问题描述】:嗯,我的大脑正在融化......我正在努力完成以下工作:
我知道有多少个数组以及每个数组有多少个元素。 这些数字是动态的,但假设有:3 个数组,每个数组有 18 个元素。
例子:
["106","142","112","77","115","127","87","127","156","118","91","93","107","151","110","79","40","186"]
["117","139","127","108","172","113","79","128","121","104","105","117","139","109","137","109","82","137"]
["111","85","110","112","108","109","107","89","104","108","123","93","125","174","129","113","162","159"]
现在我想获得所有三个数组的元素 1 的平均值,以及所有三个数组的元素 2 的平均值,依此类推。
最终结果应该是一个具有所有 18 个元素的平均值的数组。
类似:
var result_array = [];
for (i = 0; i < 3; i++)
result_array.push(arrayone[i] + arraytwo[i] + arraythree[i]) / 3
如果 3 是固定的,这将起作用,但数组的数量是动态的。
希望这是有道理的......
【问题讨论】:
数组的大小总是一样吗? 是的,每个循环都有相同的大小。但是数组的数量每次都不一样.. 实际上数组来自逗号分隔项的数组。使用拆分使它们成为数组。在 for 循环中... 【参考方案1】:这里的答案是使用循环。我们将您的数组称为 arr1、arr2 和 arr3。
var averages = [];
for(i = 0; i < arr1.length; i++)
var a = arr1[i];
var b = arr2[i];
var c = arr3[i];
var avg = (a + b + c) / 3;
averages.push(avg);
对于此循环的每次迭代,它将: - 将每个数组的下一位分配给一个变量(从索引 0 开始) - 求三个数字的平均值 -将计算结果添加到averages数组中
【讨论】:
这个问题实际上要求可变数量的数组,这是 OP 无法做到的。问题中已经几乎完全复制了此代码。【参考方案2】:用 Java 编写了这个解决方案,但您可以获得逻辑方面的帮助。希望对您有所帮助。
ArrayList<Integer> averageArrayList = new ArrayList<>();
int arrayListLength = arrayList1.length(); //assuming previous arraylists have same size.
for(int i=0; i<arrayListLength; i++)
int averageValue = (arrayList1.get(i) + arrayList2.get(i) + arrayList3.get(i)) / 3;
//adds average value to current index.
averageArrayList.add(i, averageValue);
//averageArrayList is ready with your values..
【讨论】:
【参考方案3】:var arrays = [
[106,142,112,77,115,127,87,127,156,118,91,93,107,151,110,79,40,186],
[117,139,127,108,172,113,79,128,121,104,105,117,139,109,137,109,82,137],
[111,85,110,112,108,109,107,89,104,108,123,93,125,174,129,113,162,159],
[104,153,110,112,108,109,107,89,104,108,123,93,125,174,129,113,162,159]
/* Can be any amount of arrays */
],
result = [];
//Rounding to nearest whole number.
for(var i = 0; i < arrays[0].length; i++)
var num = 0;
//still assuming all arrays have the same amount of numbers
for(var i2 = 0; i2 < arrays.length; i2++)
num += arrays[i2][i];
result.push(Math.round(num / arrays.length));
alert(result);
【讨论】:
【参考方案4】:此函数将获取任意数量的数组并计算出它们的平均值。它使用 + 运算符自动将字符串转换为数字。它是动态的,因为它不关心数组的长度或数组的数量,只要它们的长度都相等。由于我们可以假设它们都是相同的长度(基于问题),它迭代第一个数组的长度。它没有圆,因为没有要求。
function average_of_arrays(...arrays)
let result = [];
for(let array_index in arrays[0])
let total = 0;
for (let arr of arrays)
total += +arr[array_index]
result.push(total / arrays.length);
return result;
let arr1 = ["106", "142", "112", "77", "115", "127", "87", "127", "156", "118", "91", "93", "107", "151", "110", "79", "40", "186"];
let arr2 = ["117", "139", "127", "108", "172", "113", "79", "128", "121", "104", "105", "117", "139", "109", "137", "109", "82", "137"];
let arr3 = ["111", "85", "110", "112", "108", "109", "107", "89", "104", "108", "123", "93", "125", "174", "129", "113", "162", "159"];
function average_of_arrays(...arrays)
let result = [];
for(let array_index in arrays[0])
let total = 0;
for (let arr of arrays)
total += +arr[array_index]
result.push(total / arrays.length);
return result;
console.log(average_of_arrays(arr1, arr2, arr3));
【讨论】:
【参考方案5】:var result = getAvg( [ 1, 2, 3 ], [ 2, 3, 4 ] )
console.log( result ) // [ 1.5, 2.5, 3.5 ]
function getAvg()
var a = arguments
var nar = a.length
var nel = a[ 0 ].length
var el, ar, avg, avgs = []
for( el = 0; el < nel; ++el )
avg = 0
for( ar = 0; ar < nar; ++ar ) avg += a[ ar ][ el ]
avgs[ el ] = avg / nar
return avgs
【讨论】:
【参考方案6】:您确实使用 underscore.js 标记了这个问题,因此您可以使用 _.zip 方法。这会将数组中的所有第一个元素放在一起,依此类推。然后,您可以平均每个数组。
见CodePen。
var arr1 = ["106","142","112","77","115","127","87","127","156","118","91","93","107","151","110","79","40","186"]
var arr2 = ["117","139","127","108","172","113","79","128","121","104","105","117","139","109","137","109","82","137"]
var arr3 = ["111","85","110","112","108","109","107","89","104","108","123","93","125","174","129","113","162","159"]
// ... as many more arrays as you want
var avgEmAll = function (arrays)
// zip with array of arrays https://***.com/a/10394791/327074
return _.zip.apply(null, arrays).map(avg)
// average an array https://***.com/a/10624256/327074
var avg = function (x)
return x.reduce(function (y, z) return Number(y) + Number(z)) / x.length
console.log(avgEmAll([arr1, arr2, arr3]))
使用 ES6 箭头函数 (CodePen):
const avgEmAll = arrays => _.zip.apply(null, arrays).map(avg)
const sum = (y, z) => Number(y) + Number(z)
const avg = x => x.reduce(sum) / x.length
【讨论】:
以上是关于将多个数组的每一项平均到一个数组中的主要内容,如果未能解决你的问题,请参考以下文章