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