From: Jim Carter Date: 2011-02-02T07:13:49+09:00 Subject: Re: Returning a column vector from a 2D array Hope this isn't too fragmentary and of no use Here is where I create class for generating 2D arrays ****** 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 end ****** In a separate class, ExteriorBallistics, in its method "calculate," I create a new array component ****** nrows = ((los_rng - start_rng)/rng_incr).round # initialize arrays e = Array2D.new(nrows+1, 14) ****** Later I assign to the matrix e. Putting puts in front of any of these assignments yields correct column values ****** # Store Values at appropriate intervals (integral numbers of RangeInc) if ((range_ctr % rng_incr == 0)||(range_ctr == los_rng )) @index = (((rng_incr + range2 - start_rng)/rng_incr) - 1).ceil e[@index,RANGE] = range2 e[@index,VELOCITY] = vel e[@index,TIME] = time1 e[@index,PATH] = height1 * 12 # %path, converted to inches e[@index,DEFLECTION] = deflection1 * 12 # %convert to inches e[@index,ENERGY] = energy e[@index,DROP] = drop e[@index,MOA] = moa e[@index,ADJ_PATH] = adj_path e[@index,EQV_DROP] = eqv_drop e[@index,MOMENTUM] = momentum e[@index,TAYLOR_KO] = taylor_ko e[@index,OGW] = optimal_game_weight end # if (range_ctr % rng_incr == 0) ******* @index gives me the column length once the entire routine is complete. Towards the end of the "calculate" method, once all elements have been assigned to the matrix, I make the following association ******* @data_array = e ******* Furthermore, under the same class ExteriorBallistics, I declare an attr_accessor :data_array Then I want to run calculate via ExteriorBallistics.new do {a bunch of assignments here} calculate ary_length = @index #returns the correct column length puts @data_array This returns the Array ID. I can do a .inspect to have the entire matrix printed out in string format, but I really want column numeric values. Strangely, if I do either this @data_array[0..ary_length,0] or this @data_array[0,0..ary_length], I get the exact same result, which is the first ROW spanning all columns that I created. -- Posted via http://www.ruby-forum.com/.