Skip to content

How to transpose a matrix

Monadic ⍉ swaps rows and columns:

      M ← 3 4⍴⍳12
      ⍉M
1 5  9
2 6 10
3 7 11
4 8 12

The shape changes from 3 4 to 4 3:

      ⍴⍉M
4 3

Transposing a vector returns it unchanged:

      ⍉ 1 2 3
1 2 3

Transpose each matrix in a 3D array

Use the rank operator to transpose at rank 2:

      (⍉⍤2) 2 3 4⍴⍳24

See also: Apply a function to matrices