logo

Transposing General Matrices in Julia 📂Julia

Transposing General Matrices in Julia

Code

julia> X = rand(0:9, 3, 3)
3×3 Matrix{Int64}:
 9  4  5
 4  8  9
 8  7  4

julia> X'
3×3 adjoint(::Matrix{Int64}) with eltype Int64:
 9  4  8
 4  8  7
 5  9  4

In Julia, the transpose matrix can be obtained using the transpose or adjoint function, and it can even be obtained simply through the ' operator.

julia> Y = rand('0':'9', 3, 3)
3×3 Matrix{Char}:
 '8'  '3'  '8'
 '4'  '0'  '8'
 '2'  '3'  '9'

julia> Y'
3×3 adjoint(::Matrix{Char}) with eltype Union{}:
ERROR: MethodError: no method matching adjoint(::Char)

However, if the matrix is not an ordinary matrix belonging to $\mathbb{R}^{m \times n}$ or $\mathbb{C}^{m \times n}$ but a general two-dimensional array, that is, a matrix of some other data type, the transpose is not defined as above and an error occurs. In this case, the transpose can be obtained using the permutedims function as follows.

julia> permutedims(Y)
3×3 Matrix{Char}:
 '8'  '4'  '2'
 '3'  '0'  '3'
 '8'  '8'  '9'