Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting average of every item of multiple arrays into one array

Well my brains are melting... I am trying to accomplish the following:

I know how many arrays and how many elements each array have. These numbers are dynamic, but lets say there's: 3 arrays with 18 elements in each.

Example:

["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"]

Now I want to get the average of element 1 of all three arrays, and element 2 of all three and so on.

The end result should be one array with the average of all 18 elements.

Something like:

var result_array = [];
for (i = 0; i < 3; i++) {  
  result_array.push(arrayone[i] + arraytwo[i] + arraythree[i]) / 3
}

This would work if 3 was fixed, but the amount of arrays is dynamic.

Hope this make sense...

like image 777
Martin Lyder Avatar asked Aug 30 '17 21:08

Martin Lyder


2 Answers

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);
like image 185
Craig Smith Avatar answered Sep 22 '22 18:09

Craig Smith


You did tag this question with underscore.js, so you can use the _.zip method. This puts all the first elements in an array together and so on. You can then average each of those arrays.

See 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://stackoverflow.com/a/10394791/327074
  return _.zip.apply(null, arrays).map(avg)
}

// average an array https://stackoverflow.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]))

With ES6 arrow functions (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
like image 40
icc97 Avatar answered Sep 22 '22 18:09

icc97