Page 1 of 1

transpose rows into columns

Posted: Fri Jan 22, 2016 11:05 pm
by tester
1) How to display whole transposed (rows into colums) matrix?
1a) How to display (as int) amount of columns after transpose?

2) How to approach to this, if input contains rows with different element sizes?

Re: transpose rows into columns

Posted: Sat Jan 23, 2016 12:46 am
by KG_is_back
Unfortunately flowstone ruby doesn't support standard ruby library, which contains Matrix class. Here is the solution - modified version of what you have in your schematic. It works also with arrays, which have different lengths of lines, or even missing values.

Code: Select all


def transpose(matrix)
   #find longest line( to determine number of raws)
   ln=matrix.max{|line1,line2| line1.size<=>line2.size}.size
   trn=Array.new(ln){|i|
   matrix.map{|line| line[i]}
   }
   return trn
end

asd1 = @in1.map do |row|
row.split('^')
end
watch "old",asd1
asd2 = transpose(asd1)
watch "new",asd2
output 0, asd2[@in2]
output 1, asd2.map{|line| line.join("^")}


to find the length of the longest line (the number of columns) use:

Code: Select all

matrix.max{|line1,line2| line1.size<=>line2.size}.size

Re: transpose rows into columns

Posted: Sat Jan 23, 2016 10:30 am
by tester
Thanks KG!