permutedims

permutedims(A, perm)

Permute the dimensions of array A. perm is a vector specifying a permutation of length ndims(A). This is a generalization of transpose for multi-dimensional arrays. Transpose is equivalent to permutedims(A, [2,1]).

Examples

  1. Permute dimensions of a matrix:

    julia> A = [1 2 3; 4 5 6; 7 8 9];
    julia> permutedims(A, [2, 1])
    3×3 Matrix{Int64}:
    1  4  7
    2  5  8
    3  6  9

    This example permutes the dimensions of the matrix A using the provided permutation vector [2, 1]. The rows become columns and the columns become rows.

  2. Permute dimensions of a 3D array:

    julia> A = reshape(1:8, 2, 2, 2);
    julia> permutedims(A, [3, 1, 2])
    2×2×2 Array{Int64, 3}:
    [:, :, 1] =
    1  3
    2  4
    
    [:, :, 2] =
    5  7
    6  8

    In this example, a 3D array A is reshaped using the permutation vector [3, 1, 2]. The dimensions are rearranged such that the original third dimension becomes the first, the original first dimension becomes the second, and the original second dimension becomes the third.

  3. Permute dimensions of a vector:

    julia> v = [1, 2, 3, 4];
    julia> permutedims(v, [1])
    4×1 Matrix{Int64}:
    1
    2
    3
    4

    This example shows that even though a vector is technically a 2D array, permuting its dimensions does not change its structure. The permutation vector [1] results in the same vector.

  4. Transpose a matrix using permutedims:
    julia> A = [1 2 3; 4 5 6];
    julia> permutedims(A, [2, 1])
    3×2 Matrix{Int64}:
    1  4
    2  5
    3  6

    The permutedims function can be used to achieve the transpose of a matrix. In this example, the matrix A is transposed by permuting its dimensions using [2, 1].

It's worth noting that the perm vector provided to permutedims should be a valid permutation of length ndims(A). If the perm vector contains duplicate values or values outside the valid range, an error will occur.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: