# Complete Newbie - Matrix Question

**URL:** <https://rubytalk.org/t/complete-newbie-matrix-question/33632>\
**Category:** ruby-talk\
**Created:** [10 December 2006 23:10 UTC](https://rubytalk.org/t/complete-newbie-matrix-question/33632 "2006-12-10T23:10:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@Jay](https://rubytalk.org/u/Jay)\
**Post date:** [10 December 2006 23:10 UTC](https://rubytalk.org/t/complete-newbie-matrix-question/33632/1 "2006-12-10T23:10:05Z")

</div>

Hi Folks,  
This is probably quite easy, but I can't find a Matrix function or way to populate a specific element of a matrix with a given value? ie, I want to tell that element i,j of matrix.a should be 3  
My code below is a simple test to populate a 5 by 5 matrix from values in a CSV text file. The loops etc are messy, but it's just an example.

require "mathn"  
i=1  
j=1  
a = Matrix.zero(5)  
File.open("TestData.txt").each do |record|  
&nbsp;&nbsp;&nbsp;&nbsp;record.split(",").each do |field|  
&nbsp;&nbsp;&nbsp;&nbsp;field.chomp!  
&nbsp;&nbsp;&nbsp;&nbsp;if i\<5 then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix.a(i,j)=field # \<-------------How do I do this line?  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;if i = 5 then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=j+1  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;i=i+1  
&nbsp;&nbsp;&nbsp;end  
end  
puts a.to\_s

Any Help/pointers would be great.  
TIA.  
Jay.

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [10 December 2006 23:35 UTC](https://rubytalk.org/t/complete-newbie-matrix-question/33632/2 "2006-12-10T23:35:06Z")

</div>

Jay wrote:

> **···**
>
> > Hi Folks,  
> > This is probably quite easy, but I can't find a Matrix function or way  
> > to populate a specific element of a matrix with a given value? ie, I  
> > want to tell that element i,j of matrix.a should be 3
> 
> -------------------------------
> 
> #!/usr/bin/ruby -w
> 
> require 'mathn'
> 
> class Matrix  
> &nbsp;&nbsp;&nbsp;def =(i,j,v)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows[i][j] = v  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> m = Matrix.zero(5)
> 
> m[2,2] = 3
> 
> p m
> 
> -------------------------------
> 
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Brock\_Lee](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Brock\_Lee](https://rubytalk.org/u/Brock_Lee)\
**Post date:** [10 December 2006 23:55 UTC](https://rubytalk.org/t/complete-newbie-matrix-question/33632/3 "2006-12-10T23:55:04Z")

</div>

Based on the documentation at ruby-doc.org, I would think that the  
following would work for versions 1.8.5 and later. But my version of  
Ruby, 1.8.4, does not seem to support it. Here it is:

&nbsp;&nbsp;&nbsp;&nbsp;a[i, j] = field

One potential solution would be to put your data into an Array  
initially, and then convert it to a Matrix at the end. Assuming a is  
an Array, the following would work:

&nbsp;&nbsp;&nbsp;&nbsp;m = Matrix.rows(a)

By the way, a bit further down you have a conditional:

&nbsp;&nbsp;&nbsp;&nbsp;if i = 5

which should be:

&nbsp;&nbsp;&nbsp;&nbsp;if i == 5

Brock Lee

Jay wrote:

> **···**
>
> > Hi Folks,  
> > This is probably quite easy, but I can't find a Matrix function or way  
> > to populate a specific element of a matrix with a given value? ie, I  
> > want to tell that element i,j of matrix.a should be 3  
> > My code below is a simple test to populate a 5 by 5 matrix from values  
> > in a CSV text file. The loops etc are messy, but it's just an example.
> > 
> > require "mathn"  
> > i=1  
> > j=1  
> > a = Matrix.zero(5)  
> > File.open("TestData.txt").each do |record|  
> > &nbsp;&nbsp;&nbsp;&nbsp;record.split(",").each do |field|  
> > &nbsp;&nbsp;&nbsp;&nbsp;field.chomp!  
> > &nbsp;&nbsp;&nbsp;&nbsp;if i\<5 then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix.a(i,j)=field # \<-------------How do I  
> > do this line?  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;if i = 5 then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=j+1  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;i=i+1  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > puts a.to\_s
> > 
> > Any Help/pointers would be great.  
> > TIA.  
> > Jay.

---

<div class="post-metadata">

**Author:** ![Brock\_Lee](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Brock\_Lee](https://rubytalk.org/u/Brock_Lee)\
**Post date:** [10 December 2006 23:55 UTC](https://rubytalk.org/t/complete-newbie-matrix-question/33632/4 "2006-12-10T23:55:05Z")

</div>

Based on the documentation at ruby-doc.org, I would think that the  
following would work for versions 1.8.5 and later. But my version of  
Ruby, 1.8.4, does not seem to support it. Here it is:

&nbsp;&nbsp;&nbsp;&nbsp;a[i, j] = field

One potential solution would be to put your data into an Array  
initially, and then convert it to a Matrix at the end. Assuming a is  
an Array, the following would work:

&nbsp;&nbsp;&nbsp;&nbsp;m = Matrix.rows(a)

By the way, a bit further down you have a conditional:

&nbsp;&nbsp;&nbsp;&nbsp;if i = 5

which should be:

&nbsp;&nbsp;&nbsp;&nbsp;if i == 5

Brock Lee

Jay wrote:

> **···**
>
> > Hi Folks,  
> > This is probably quite easy, but I can't find a Matrix function or way  
> > to populate a specific element of a matrix with a given value? ie, I  
> > want to tell that element i,j of matrix.a should be 3  
> > My code below is a simple test to populate a 5 by 5 matrix from values  
> > in a CSV text file. The loops etc are messy, but it's just an example.
> > 
> > require "mathn"  
> > i=1  
> > j=1  
> > a = Matrix.zero(5)  
> > File.open("TestData.txt").each do |record|  
> > &nbsp;&nbsp;&nbsp;&nbsp;record.split(",").each do |field|  
> > &nbsp;&nbsp;&nbsp;&nbsp;field.chomp!  
> > &nbsp;&nbsp;&nbsp;&nbsp;if i\<5 then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix.a(i,j)=field # \<-------------How do I  
> > do this line?  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;if i = 5 then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=j+1  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;i=i+1  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > puts a.to\_s
> > 
> > Any Help/pointers would be great.  
> > TIA.  
> > Jay.

---

<div class="post-metadata">

**Author:** ![Jose\_Augusto1](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jose\_Augusto1](https://rubytalk.org/u/Jose_Augusto1)\
**Post date:** [11 December 2006 00:23 UTC](https://rubytalk.org/t/complete-newbie-matrix-question/33632/5 "2006-12-11T00:23:09Z")

</div>

Hi

Once I had the same problem and proposed the following functions  
to be appended to standard matrix and vector classes (but they  
weren't ;-( ):

> **···**
>
> #  
> # Extending standard Matrix and Vector Classes to write directly  
> #  
> class Matrix
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# setting one single element  
> &nbsp;&nbsp;&nbsp;&nbsp;def =(i, j, val)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows[i][j] = val  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# setting one row  
> &nbsp;&nbsp;&nbsp;&nbsp;def set\_row(i,\*row)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case row[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when Vector  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows[i] = row[0].to\_a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when Array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows[i] = row[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else # list of scalars  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows[i] = row  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# setting one column  
> &nbsp;&nbsp;&nbsp;&nbsp;def set\_column(j,\*column)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case column[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when Vector, Array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows.collect{|row|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row[j] = column[0][j]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows.collect{|row|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row[j] = column[j]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> end
> 
> class Vector  
> &nbsp;&nbsp;&nbsp;&nbsp;# setting one single element  
> &nbsp;&nbsp;&nbsp;&nbsp;def =(i, val)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@elements[i] = val  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# clear vector  
> &nbsp;&nbsp;&nbsp;&nbsp;def Vector.zero(n)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new(:init\_elements, Array.new(n,0),  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy = false)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> Hope this helps...
> 
> JA
> 
> On 12/10/06, Jay \<j@somewhere.com\> wrote:
> 
> > Hi Folks,  
> > This is probably quite easy, but I can't find a Matrix function or way  
> > to populate a specific element of a matrix with a given value? ie, I  
> > want to tell that element i,j of matrix.a should be 3  
> > My code below is a simple test to populate a 5 by 5 matrix from values  
> > in a CSV text file. The loops etc are messy, but it's just an example.
> > 
> > require "mathn"  
> > i=1  
> > j=1  
> > a = Matrix.zero(5)  
> > File.open("TestData.txt").each do |record|  
> > &nbsp;&nbsp;&nbsp;&nbsp;record.split(",").each do |field|  
> > &nbsp;&nbsp;&nbsp;&nbsp;field.chomp!  
> > &nbsp;&nbsp;&nbsp;&nbsp;if i\<5 then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix.a(i,j)=field # \<-------------How do I  
> > do this line?  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;if i = 5 then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=j+1  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;i=i+1  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > puts a.to\_s
> > 
> > Any Help/pointers would be great.  
> > TIA.  
> > Jay.
