I\'ve got two (or more) arrays with 12 integers in each (corresponding to values for each month). All I want is to add them together so that I\'ve got a single array with su
For clearer syntax (not the fastest), you can make use of Vector
:
require 'matrix'
Vector[1,2,3] + Vector[4,5,6]
=> Vector[5, 7, 9]
For multiple vectors, you can do:
arr = [ Vector[1,2,3], Vector[4,5,6], Vector[7,8,9] ]
arr.inject(&:+)
=> Vector[12, 15, 18]
If you wish to load your arrays into Vectors and sum:
arrays = [ [1,2,3], [4,5,6], [7,8,9] ]
arrays.map { |a| Vector[*a] }.inject(:+)
=> Vector[12, 15, 18]