6

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...

icc97
  • 11,395
  • 8
  • 76
  • 90
Martin Lyder
  • 169
  • 2
  • 12

6 Answers6

6

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);
Craig Smith
  • 210
  • 1
  • 7
1

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
icc97
  • 11,395
  • 8
  • 76
  • 90
0

The answer here is to use a loop. Let's call your arrays arr1, arr2, and 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);
}

For each iteration of this loop, it will:
-Assign the next digit of each array to a variable (starting at index 0)
-Find the average of the three numbers
-Add the result of the calculation to the averages array

abagshaw
  • 6,162
  • 4
  • 38
  • 76
Steve Decker
  • 31
  • 1
  • 9
  • The question actually asks for a variable number of arrays which is the bit the OP couldn't do. There was almost an exact duplicate of this code in the question already. – icc97 Aug 31 '17 at 08:34
0

Wrote this solution in Java but you can get help with logic. Hope it helps.

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..
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31
0

This function will take any number of arrays and figure out their average. It uses the + operator to automatically cast the string to a number. It is dynamic in that it doesn't care what length the arrays are, or how many there are, as long as they are all equal in length. Since we can assume that they are all the same length( based on the question ) it iterates over the length of the first array. It doesn't round because that wasn't asked for.

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));
zfrisch
  • 8,474
  • 1
  • 22
  • 34
0
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

}
flcoder
  • 713
  • 4
  • 14