1

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

arjun
  • 1,594
  • 16
  • 33
  • 2
    What do you expect the output to look like? Matrix addition is only defined when the two matrices have the same dimensions, as you can guess from the error. – Mickey Sheu Jul 03 '17 at 21:07
  • In the python one, `arr2` is added to each row of `arr1`. – arjun Jul 04 '17 at 05:09

1 Answers1

3

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.

Thermatix
  • 2,757
  • 21
  • 51