Revision d5bfb0e3b7a3211ff2bbe2926e5ba31aa24e4df6 authored by HwB on 09 March 2012, 00:00:00 UTC, committed by Gabor Csardi on 09 March 2012, 00:00:00 UTC
1 parent 63e8a52
combs.R
##
## c o m b s . R Combinations
##
combs <- function(a, m){
n <- length(a)
if (length(a) == 0 || m <= 0) return(c())
if (m >= n) return(a)
if (m <= 1) return(matrix(a, n, 1))
v <- c(a)
P <- c()
for (k in 1:(n-m)) {
Q <- combs(v[(k+1):n], m-1)
P <- rbind(P, cbind(v[k], Q))
}
k <- n-m+1
Q <- combs(v[(k+1):n], m-1)
P <- rbind(P, c(v[k], Q))
b <- a[c(P)]
dim(b) <- dim(P)
return(P)
}
randcomb <- function(a, m) {
n <- length(a)
if (n == 0) return(c())
m <- sample(1:n, size = m, replace = FALSE)
return(a[m])
}
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...