permute!

permute!(v, p)

Permute vector v in-place, according to permutation p. No checking is done to verify that p is a permutation.

To return a new permutation, use v[p]. Note that this is generally faster than permute!(v,p) for large vectors.

Examples

  1. Permute elements of an array:

    julia> arr = [1, 2, 3, 4];
    julia> permute!(arr, [4, 3, 2, 1])
    4-element Array{Int64,1}:
    4
    3
    2
    1

    This example permutes the elements of the array arr according to the permutation [4, 3, 2, 1].

  2. Permute elements of a string:

    julia> str = "julia";
    julia> permute!(str, [2, 1, 5, 4, 3, 6])
    "ujailj"

    It permutes the characters of the string str according to the given permutation.

  3. Permute elements of a matrix column:
    julia> matrix = [1 2 3; 4 5 6; 7 8 9];
    julia> permute!(matrix[:, 2], [3, 1, 2])
    3-element Array{Int64,1}:
    8
    2
    5

    This example permutes the elements of the second column of the matrix matrix according to the permutation [3, 1, 2].

Common mistake example:

julia> arr = [1, 2, 3, 4];
julia> permute!(arr, [3, 2, 1, 5])
ERROR: BoundsError: attempt to access 4-element Array{Int64,1} at index [5]

In this example, the permutation provided has an index that is out of bounds for the array arr. Make sure the permutation is a valid mapping for the elements in the collection to avoid such errors.

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: