# Matrix problem

**URL:** <https://www.ruby-forum.com/t/matrix-problem/109224>\
**Category:** Ruby\
**Created:** [July 12, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/matrix-problem/109224 "2007-07-12T15:29:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [July 12, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/matrix-problem/109224/1 "2007-07-12T15:29:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [July 12, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/matrix-problem/109224/2 "2007-07-12T16:06:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [July 12, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/matrix-problem/109224/3 "2007-07-12T16:21:23Z")

</div>

On 7/12/07, Bulhac M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
