From: Jose Augusto Date: 2006-12-11T09:23:09+09:00 Subject: Re: Complete Newbie - Matrix Question ------=_Part_100216_24345078.1165796587734 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 On 12/10/06, 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. > > > > ------=_Part_100216_24345078.1165796587734--