Matrix transposition: Difference between revisions

m (Moved to Matrix cat)
Line 284:
for i in 1 to 5 do for j in 1 to 4 do m[i][j] = pow i j
m = transpose m
 
=={{header|OCaml}}==
 
Matrices can be represented in OCaml as a type <code>'a array array</code>, or using the module [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Bigarray.html Bigarray].
The implementation below uses a bigarray:
 
<ocaml>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) ;;</ocaml>
 
This will output:
1 5
2 6
3 7
4 8
 
 
=={{header|Perl}}==