Finding the sum of 2D Arrays in Ruby
- by Bragaadeesh
Hi,
I have an array of two dimensional Arrays. I want to create a new two dimensional array which finds the sum of these values in the 2D arrays.
Sum at x,y of new array = Sum at x,y of arr1 + Sum at x,y of arr2 + ....
|1,2,4| |1,1,1| |1,1,1|
|2,4,6| |1,1,1| |1,1,1|
|2,4,6| |1,1,1| |1,1,1|
|2,4,6| |1,1,1| |1,1,1|
Now adding the above two dimensional arrays will result in,
|3,4,6|
|4,6,8|
|4,6,8|
|4,6,8|
How to achieve this in Ruby (not in any other languages). I have written a method, but it looks very long and ugly.