[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Complete Newbie - Matrix Question

Jay

12/10/2006 11:07:00 PM

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.


3 Answers

Paul Lutus

12/10/2006 11:34:00 PM

0

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

-------------------------------

--
Paul Lutus
http://www.ara...

Brock Lee

12/10/2006 11:51:00 PM

0

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:

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


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

Brock Lee

12/10/2006 11:51:00 PM

0

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:

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


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