Todd Benson
7/12/2007 2:18:00 PM
On 7/12/07, Bulhac Mihai <mihai.bulhac@yahoo.com> wrote:
> i have a matrix :remember=Matrix[ [] ]
> and this line remember[0,0]=3
> why do i have this error at that line: undefined method `[]=' for
> Matrix[[]]:Matrix (NoMethodError)
>
> and how do i set values for my matrix only with one row
Hmm. It appears Matrix objects are supposed to be immutable. Use
arrays instead and convert to Matrix for matrix-type functions, so ...
irb> require 'matrix'
=> true
irb> require 'mathn' #need this for correct determinant calculations
=> true
irb> a = [[1, 2], [3, 4]]
=> [[1, 2], [3, 4]]
irb> a[0][0] = 3
=> [[3, 2], [3, 4]]
irb> m = Matrix[*a]
=> Matrix[[3, 2], [3, 4]]
irb> m.transpose
=> Matrix[[3, 3], [2, 4]]
irb> m * m
=> Matrix[[15, 14], [21, 22]]
irb> m_float = m.map {|i| i.to_f}
=> Matrix[[3.0, 2.0], [3.0, 4.0]]
irb> m_inv = m_float.inverse
=> Matrix[[0.666666666666667, -0.333333333333333], [-0.5, 0.5]]
irb> m.to_a
=> [[0.666666666666667, -0.333333333333333], [-0.5, 0.5]]
hth,
Todd