In Javascript, if I have an array of arrays representing a matrix, say
x = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
];
summing it
Really elegant only for square matrix from me.
x = [[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
];
var sum = function(arr) {
return arr.reduce(function(a, b){ return a + b; }, 0);
};
x.map(function(row, i) {
return sum(x.map(function(row) { return row[i]; }));
});
http://jsfiddle.net/btux9s2d/ example with console log.
for any size matrix, not so elegant, but maybe will help you on your way: http://jsfiddle.net/btux9s2d/2/