Finding the sum of 2D Arrays in Ruby

Posted by Bragaadeesh on Stack Overflow See other posts from Stack Overflow or by Bragaadeesh
Published on 2010-06-10T11:36:57Z Indexed on 2010/06/10 12:13 UTC
Read the original article Hit count: 203

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about sum