Logo

Programming-Idioms

  • JS
  • Pascal

Idiom #193 Transpose a two-dimensional matrix

Declare two two-dimensional arrays a and b of dimension n*m and m*n, respectively. Assign to b the transpose of a (i.e. the value with index interchange).

uses typ, omv;
var
  A: array[1..m, 1..n] of ArbFloat;
  B: array[1..n, 1..m] of ArbFloat;
begin
  ... some code to fill A
  omvtrm(
    A[1,1], m, n, n,
    B[1,1], m
  );
end.
const a = [[1, 2, 3], [4, 5, 6]]
const m = a[0].length
const b = Array.from({ length: m }, (_, n) => a.map(row => row[n]))

Requires ES2015
(def a [[1 2 3] [4 5 6] [7 8 9]])
(def b (apply (partial mapv vector) a))

New implementation...
< >
tkoenig