Ruby matrix addition to multi dimensional array

257 Views Asked by At

Multidimensional Matrix array is like this

arr1 = Matrix[[0.9742006046104146, 0.9164380106962612, 0.39571440216724874],  
              [1.3793903493310324, 1.8988033906016721, 1.2768961254764901], 
              [0.42334074004480604, 1.6728495387871951, 1.2575501206006443]]

Another simple array is like this

arr2 = Matrix[[0.13054527963360518, 0.8579042642337861, 0.3041160868559809]]

I can't add both together, arr1 + arr2

ExceptionForMatrix::ErrDimensionMismatch: Matrix dimension mismatch
    from /home/arjun/.rbenv/versions/2.2.3/lib/ruby/2.2.0/matrix.rb:996:in `+'
    from (irb):171
    from /home/arjun/.rbenv/versions/2.2.3/bin/irb:11:in `<main>'

How can I do it. In python/numpy simply doing an addition just works?
What is the Ruby equivalent?

NOTE -
arr1 is the dot product of Matrix[*a] * Matrix[*b], if that matters

1

There are 1 best solutions below

0
On

I'm guessing you'd have to push the contents of the second matrix into the first matrix, maybe with either the spade operator matrix_1 << matrix_2[0]or the .push method.

p.s. I'd like to add, I've never used Matrix in ruby hence my 'guess' but since it's array like It's fair to assume that it has an array like API.