Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS calculate mean of same elements in 2d array

Tags:

javascript

I've got a 'table' of two columns represented as an array. The first column are numbers from 1 to 20 and they are labels, the second column are the corresponding values (seconds):

my_array = [ [ 3,4,5,3,4,5,2 ],[ 12,14,16,11,12,10,20 ] ];

I need the mean (average) for each label:

my_mean_array = [ [ 2,3,4,5 ],[ 20/1, (12+11)/2, (14+12)/2, (16+10)/2 ] ];
// edit: The mean should be a float - the notion above is just for clarification.
// Also the number 'labels' should remain as numbers/integers.

My try:

var a = my_array[0];
var b = my_array[1];
m = [];
n = [];
for( var i = 0; a.length; i++){
    m[ a[i] ] += b[i]; // accumulate the values in the corresponding place
    n[ a[i] ] += 1; // count the occurences
}
var o = [];
var p = [];
o = m / n;
p.push(n);
p.push(o);
like image 370
Chrugel Avatar asked Feb 13 '13 07:02

Chrugel


1 Answers

How about this (native JS, will not break on older browsers):

function arrayMean(ary) {
  var index = {}, i, label, value, result = [[],[]];

  for (i = 0; i < ary[0].length; i++) {
    label = ary[0][i];
    value = ary[1][i];
    if (!(label in index)) {
      index[label] = {sum: 0, occur: 0};
    }
    index[label].sum += value;
    index[label].occur++;
  }
  for (i in index) {
    if (index.hasOwnProperty(i)) {
      result[0].push(parseInt(i, 10));
      result[1].push(index[i].occur > 0 ? index[i].sum / index[i].occur : 0);
    }
  }
  return result;
}

FWIW, if you want fancy I've created a few other ways to do it. They depend on external libraries and are very probably an order of magnitude slower than a native solution. But they are nicer to look at.

It could look like this, with underscore.js:

function arrayMeanUnderscore(ary) {
  return _.chain(ary[0])
    .zip(ary[1])
    .groupBy(function (item) { return item[0]; })
    .reduce(function(memo, items) {
      var values = _.pluck(items, 1),
          toSum = function (a, b) { return a + b; };

      memo[0].push(items[0][0]);
      memo[1].push(_(values).reduce(toSum) / values.length);
      return memo;
    }, [[], []])
    .value();
}

// --------------------------------------------

arrayMeanUnderscore([[3,4,5,3,4,5,2], [12,14,16,11,12,10,20]]);
// -> [[2,3,4,5], [20,11.5,13,13]]

or like this, with the truly great linq.js (I've used v2.2):

function arrayMeanLinq(ary) {
  return Enumerable.From(ary[0])
    .Zip(ary[1], "[$, $$]")
    .GroupBy("$[0]")
    .Aggregate([[],[]], function (result, item) {
      result[0].push(item.Key());
      result[1].push(item.Average("$[1]"));
      return result;
    });
}

// --------------------------------------------

arrayMeanLinq([[3,4,5,3,4,5,2], [12,14,16,11,12,10,20]]);
// -> [[3,4,5,2], [11.5,13,13,20]]

As suspected, the "fancy" implementations are an order of magnitude slower than a native implementation: jsperf comparison.

like image 142
Tomalak Avatar answered Oct 04 '22 16:10

Tomalak