ruby on rails - How do I add values in an array of arrays? -


i have array of arrays 4 integer elements this:

arrays = [[a1,b1,c1,d1],[a2,b2,c2,d2],[a3,b3,c3,d3]]  

i want add these 3 arrays. result should in 1 array like:

result = [a1+a2+a3,b1+b2+b3,c1+c2+c3,d1+d2+d3] 

i tried not able this.

>> arrays = [[1,2,3,4],[2,3,4,5],[3,4,5,6]] => [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]] >> arrays.transpose.map { |xs| xs.inject :+ } => [6, 9, 12, 15] 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -