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
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
Bulhac M. 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 7/12/07, Bulhac M. [email protected] 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs