From: Brock Lee Date: 2006-12-11T08:55:04+09:00 Subject: Re: Complete Newbie - Matrix Question 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.