ipermute!

ipermute!(v, p)

Like permute!, but the inverse of the given permutation is applied.

Examples

In the Julia programming language, the function ipermute!(v, p)

This function applies the inverse of the given permutation p to the collection v in-place.

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

Here are some common examples of how to use ipermute!:

  1. Rearrange elements of an array:

    julia> arr = [10, 20, 30, 40];
    julia> p = [4, 3, 2, 1];
    julia> ipermute!(arr, p)
    4-element Array{Int64,1}:
    40
    30
    20
    10

    This example applies the inverse permutation p to the array arr, resulting in the rearrangement of elements.

  2. Modify a vector of strings using a permutation:
    julia> words = ["apple", "banana", "orange", "grape"];
    julia> p = [2, 4, 3, 1];
    julia> ipermute!(words, p)
    4-element Array{String,1}:
    "grape"
    "apple"
    "orange"
    "banana"

    It applies the inverse permutation p to the vector of strings words, changing the order of elements accordingly.

Common mistake example:

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

In this example, the permutation p is not valid for the array arr. It's important to ensure that the permutation is a valid mapping for the collection to avoid such errors. Always double-check that the permutation indices are within the valid range of the collection before using ipermute!.

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: