Julia's Permutation Dimension Function and Its Application permutedims
Overview
The built-in function permutedims
in Julia can be seen as a generalization of transpose matrix and is useful for handling dimensions of multi-dimensional arrays.
Code
Tensor Dimension Transformation
julia> A = reshape(Vector(1:8), (2,2,2))
2×2×2 Array{Int64, 3}:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
julia> B = permutedims(A, (3, 1, 2))
2×2×2 Array{Int64, 3}:
[:, :, 1] =
1 2
5 6
[:, :, 2] =
3 4
7 8
Tensors are programmatically useful but equally confusing. Particularly in deep learning tasks related to images, this function can be very handy.
Transpose of String Matrix
julia> A = ["a" "b" "c"
"d" "e" "f"]
2×3 Matrix{String}:
"a" "b" "c"
"d" "e" "f"
julia> A'
ERROR: MethodError: no method matching adjoint(::String)
julia> permutedims(A)
3×2 Matrix{String}:
"a" "d"
"b" "e"
"c" "f"
As you can see, a matrix of strings cannot be transposed using ‘'
, but the permutedims
function can be utilized to achieve functionality equivalent to matrix transpose1.
Environment
- OS: Windows
- julia: v1.11.1