Learn R Programming

pracma (version 1.4.5)

invperm: Inverse Permutation

Description

Generates the inverse permutation.

Usage

invperm(p)

Arguments

p
permutation, i.e. the vector 1:n permuted.

Value

  • Vector of permuted elements of 1:length(p).

Details

Generates the inverse permutation.

See Also

randperm

Examples

Run this code
p1 <- c(1,3,5,2,4)
p1inv <- invperm(p1)            # 1 4 2 5 3

# Define two more functions for permutations
compoperm <- function(p, q) q[p]        # composition of permutations
compoperm(p1, p1inv)                    # 1 2 3 4 5
applyperm <- function(p, x) x[p]        # apply permutation to vector
p2 <- c(2,1,3,5,4)
x  <- c(11,12,13,14,15)
applyperm(compoperm(p2, p1), x) == applyperm(p2, applyperm(p1, x))  # all TRUE

Run the code above in your browser using DataLab