Select column-wise of a 2d array in ruby

multidimensional-array, ruby

Solution

Do as below using `#transpose` method :

A.transpose.each do |ary|
   # your code
end

As per your comment, I would suggest to use `Matrix` class. Once you will create a `Matrix` object, you can access the elements of it, row wise or column wise.

require 'matrix'

A = [['a1','a2','a3'],['b1','b2','b3'],['c1','c2','c3']]

mat = Matrix[ *A ]
mat.column(1).to_a # => ["a2", "b2", "c2"]

Problem

I have a 2d array `A = [[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]].` I want to access this array column-wise. something like that- ``` A[all][0] -> [a1,b1,c1] ``` How can i do that?

Original source