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

on 2007-07-12 15:29

on 2007-07-12 16:06

Bulhac Mihai 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 Matrix is immutable, so there is no method []= to assign values. []= is the method called in remember[0,0]=3, it is just syntax sugar for remember.[]=(0,0,3). Maybe you want nested arrays instead. Regards Stefan

on 2007-07-12 16:21

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