# Complete Newbie - Matrix Question

**URL:** <https://www.ruby-forum.com/t/complete-newbie-matrix-question/78960>\
**Category:** Ruby\
**Created:** [December 11, 2006, 12:10am UTC](https://www.ruby-forum.com/t/complete-newbie-matrix-question/78960 "2006-12-11T00:10:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [December 11, 2006, 12:10am UTC](https://www.ruby-forum.com/t/complete-newbie-matrix-question/78960/1 "2006-12-11T00:10:20Z")

</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|  
record.split(",").each do |field|  
field.chomp!  
if i\<5 then  
Matrix.a(i,j)=field # \<-------------How do I  
do this line?  
end  
if i = 5 then  
i = 1  
j=j+1  
end  
i=i+1  
end  
end  
puts a.to\_s

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

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [December 11, 2006, 12:36am UTC](https://www.ruby-forum.com/t/complete-newbie-matrix-question/78960/2 "2006-12-11T00:36:22Z")

</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  
def []=(i,j,v)  
@rows[i][j] = v  
end  
end

m = Matrix.zero(5)

m[2,2] = 3

p m

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [December 11, 2006, 12:56am UTC](https://www.ruby-forum.com/t/complete-newbie-matrix-question/78960/3 "2006-12-11T00:56:26Z")

</div>

Based on the documentation at [ruby-doc.org](http://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:

```
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:

```
m = Matrix.rows(a)

```

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

```
if i = 5

```

which should be:

```
if i == 5

```

Brock Lee

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [December 11, 2006, 1:24am UTC](https://www.ruby-forum.com/t/complete-newbie-matrix-question/78960/4 "2006-12-11T01:24:28Z")

</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

```
# setting one single element
def []=(i, j, val)
    @rows[i][j] = val
end

# setting one row
def set_row(i,*row)
    case row[0]
        when Vector
            @rows[i] = row[0].to_a
        when Array
            @rows[i] = row[0]
        else # list of scalars
            @rows[i] = row
    end
end

# setting one column
def set_column(j,*column)
    case column[0]
        when Vector, Array
            @rows.collect{|row|
                row[j] = column[0][j]
            }
        else
            @rows.collect{|row|
                row[j] = column[j]
            }
    end
end

```

end

class Vector  
# setting one single element  
def []=(i, val)  
@elements[i] = val  
end

```
# clear vector
def Vector.zero(n)
    new(:init_elements, Array.new(n,0),
        copy = false)
end

```

end

Hope this helps…

JA
