R/permutations.R

Defines functions `permutations`

`permutations` <-
function(n)
################################################################
# permutations from package e1071
################################################################
{
    if (n == 1)
        return(matrix(1))
    else if (n < 2)
        stop("n must be a positive integer")
    z <- matrix(1)
    for (i in 2:n) {
        x <- cbind(z, i)
        a <- c(1:i, 1:(i - 1))
        z <- matrix(0, ncol = ncol(x), nrow = i * nrow(x))
        z[1:nrow(x), ] <- x
        for (j in 2:i - 1) {
            z[j * nrow(x) + 1:nrow(x), ] <- x[, a[1:i + j]]
        }
    }
    dimnames(z) <- NULL
    z
}

Try the prefmod package in your browser

Any scripts or data that you put into this service are public.

prefmod documentation built on Sept. 30, 2023, 5:06 p.m.