Transpose an arbitrarily sized rectangular Matrix.




procedure transpose_matrix (matrix)
  result := []
  # for each column
  every (i := 1 to *matrix[1]) do {
    col := []
    # extract the number in each row for that column
    every (row := !matrix) do put (col, row[i]) 
    # and push that column as a row in the result matrix
    put (result, col)
  }
  return result
end
 
procedure print_matrix (matrix)
  every (row := !matrix) do {
    every writes (!row || " ")
    write ()
  }
end
 
procedure main ()
  matrix := [[1,2,3],[4,5,6]]
  write ("Start:")
  print_matrix (matrix)
  transposed := transpose_matrix (matrix)
  write ("Transposed:")
  print_matrix (transposed)
end