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
Post a Comment