RosettaCodeData/Task/Matrix-transposition/OCaml/matrix-transposition-1.ocaml

32 lines
626 B
Plaintext

open Bigarray
let transpose b =
let dim1 = Array2.dim1 b
and dim2 = Array2.dim2 b in
let kind = Array2.kind b
and layout = Array2.layout b in
let b' = Array2.create kind layout dim2 dim1 in
for i=0 to pred dim1 do
for j=0 to pred dim2 do
b'.{j,i} <- b.{i,j}
done;
done;
(b')
;;
let array2_display print newline b =
for i=0 to Array2.dim1 b - 1 do
for j=0 to Array2.dim2 b - 1 do
print b.{i,j}
done;
newline();
done;
;;
let a = Array2.of_array int c_layout [|
[| 1; 2; 3; 4 |];
[| 5; 6; 7; 8 |];
|]
array2_display (Printf.printf " %d") print_newline (transpose a) ;;