Transpose an arbitrarily sized rectangular Matrix.




import util.
 
go =>
  M = [[0.0, 0.1, 0.2, 0.3],
       [0.4, 0.5, 0.6, 0.7],
       [0.8, 0.9, 1.0, 1.1]],
  print_matrix(M),
 
  M2 = [[a,b,c,d,e],
        [f,g,h,i,j],
        [k,l,m,n,o],
        [p,q,r,s,t],
        [u,v,w,z,y]],
  print_matrix(M2),
 
  M3 = make_matrix(1..24,8),
  print_matrix(M3),
  nl.
 
 
%
% Print original matrix and its transpose
%
print_matrix(M) =>
  println("Matrix:"),
  foreach(Row in M) println(Row) end,
  println("\nTransposed:"),
  foreach(Row in M.transpose()) println(Row) end,
  nl.
 
%
% Make a matrix of list L with Rows rows 
% (and L.length div Rows columns)
%
make_matrix(L,Rows) = M =>
  M = [],
  Cols = L.length div Rows,
  foreach(I in 1..Rows)
     NewRow = new_list(Cols),
     foreach(J in 1..Cols)
       NewRow[J] := L[ (I-1)*Cols + J]
     end,
     M := M ++ [NewRow]
  end.