transpose rows into columns

For general discussion related FlowStone
Post Reply
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

transpose rows into columns

Post 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?
Attachments
transpose.fsm
(466 Bytes) Downloaded 847 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: transpose rows into columns

Post 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
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: transpose rows into columns

Post by tester »

Thanks KG!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Post Reply