Sum an array of arrays (matrices) vertically efficiently / elegantly

In Javascript, if I have an array of arrays representing a matrix, let's say

x = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ]; 

summing it horizontally is easy and can be performed as

 x.map(function(y){ return y.reduce(function(a,b){ return a+b; }); }); 

Now I would like to calculate the "vertical" amount that can be executed

 x.reduce(function(a, b){ return a.map(function(v,i){ return v+b[i]; }); }); 

But I'm not happy with this version, and I'm sorry that there was something more pleasant, more elegant and more straightforward. Maybe an easy way to transfer the matrix in advance? is anyone

Please note that I asked a similar question a few days ago ( link ), but he didn’t have a bigger picture.

+4
source share
4 answers

I think you are unhappy that there is no built-in zip function, because that is what your reduce in map does. You will have to either implement it yourself, or import it from a library, such as Ramda , to write elegant code as follows:

 var verticalSum = x.map(R.zipWith(function(a,b){ return a+b; })); 

If you are looking for an effective solution, there is basically no better way than explicit contours.

+1
source

You can sum the values ​​in the same index.

Usage: array.reduce(sum)

 var sum = (r, a) => r.map((b, i) => a[i] + b); console.log([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]].reduce(sum)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
+7
source
 arrayOfArrays = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ]; arrayOfArrays.reduce(function(array1, array2) { return array1.map(function(value, index) { return value + array2[index]; }); }); 

For my own benefit and, I hope, in the interests of others, I wanted to explain how the functions of the nested reduce / map methods perform the vertical summation of an array of arrays of the same length. This is a perfectly acceptable version offered by OP.

The reduce method applies a function against the accumulator and each array value from left to right to reduce it to one value ( MDN ).

In the case of this example, in the first iteration, the first two arrays are passed to the callback function to reduce as array1 and array2 . The return value for the callback function is the result of the map method applied to array 1.

map returns a new array with the results of calling the provided function for each element of the array. ( MDN ).

So map array1 each value in array1 and adds it to the value in array2 at the same index position. These results are transferred to a new array, which then returns to the reduce method.

Now the sum array that was just returned becomes the new array1 and reduce calls its function, passing in the new array1 , and the next array as the new array2 .

This repeats until we finish the arrays in arrayOfArrays

Beginning with:

 arrayOfArrays = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ]; 

The first iteration of abbreviations takes place:

 array1 [1,2,3,4] array2 [5,6,7,8] 

To the callback function for reduce . The return value for reduce is the new array obtained with map to add each value of array1 to the value of array2 in the same position:

 array1 [1,2,3,4] array2 [5,6,7,8] return [6,8,10,12] 

This value returns to the reduce method and becomes the new array1 . array1 and the following array ( array2 ) are then passed to reduce again and summed by the map method:

 array1 [6,8,10,12] array2 [9,10,11,12] return [15,18,21,24] // returned by map() 
+2
source

Really elegant just for the 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/ console log example.

for any dimensional matrix, not so elegant, but it may help you on your way: http://jsfiddle.net/btux9s2d/2/

+1
source

All Articles