From: Jim Carter Date: 2011-02-03T05:22:37+09:00 Subject: Re: Returning a column vector from a 2D array Using Matrix.rb does not work in this application, because I have to assign matrix values dynamically, and it appears that Matrix class requires static assignment of the matrix. That's why I created my own Array2D class. I was able to take and modify a method from Matrix.rb to solve this problem however. It is the column method (see below) class Array2D require 'matrix' def initialize(row, column) #this creates the 2D array structure @data = Array.new(row) { Array.new(column) {0}} #initialize array with #0's end def [](x, y) #this allows one read access # to cells within 2D array @data[x][y] end def []=(x, y, value) #this allows one write access to cells within 2D array @data[x][y] = value end # # Returns column vector number +j+ of the matrix as a Vector (starting at 0 # like an array). When a block is given, the elements of that vector are # iterated. # def column(j,row_size) # :yield: e if block_given? row_size.times do |i| yield @data[i][j] end else col = (0 ... row_size).collect {|i| @data[i][j] } #Vector.elements(col, false) end end #column end This is the call puts ary_length = @index puts @data_array.column(0,ary_length) -- Posted via http://www.ruby-forum.com/.