[R] AW: Permutations

Prof Brian D Ripley ripley at stats.ox.ac.uk
Sun Dec 17 13:00:52 CET 2000


Not sure why the columns are all headed "i"!  The use of rbind and cbind
makes this horribly memory-intensive, so much so that the practical
limit on my system was n=9.  Of course, beyond n=10 you would have trouble
storing the answer in a single matrix.  But, do try to avoid repeated
rbind-ing like this as it means the vector of matrix entries has to
be re-mapped every time.  Grow it in blocks (i rows at a tiem, here).

On Sun, 17 Dec 2000, Trenkler, Dietrich wrote:

> 		Niels Waller wrote: 
> 
> > Does anyone know of an R (or S-PLUS) function for delineating all possible
> > combinations and permutations?
> 
> The following function delivers all permutations of 1:n.
> 
> all.perm <- function(n) {
>     p <- matrix(1, ncol = 1)
>     for (i in 2:n) {
>         p <- pp <- cbind(p, i)
>         v <- c(1:i, 1:(i - 1))
>         for (j in 2:i) {
>             v <- v[-1]
>             p <- rbind(p, pp[, v[1:i]])
>         }
>     }
>     p
> }
> 
> 
> 
> Regards,
> 
>   *** D.Trenkler ***
> 
> ====================================================================
> Dietrich Trenkler               (trenkler at nts6.oec.uni-osnabrueck.de)
> Statistik / Empirische Wirtschaftsforschung
> Universitaet Osnabrueck
> Rolandstrasse 8                           Phone: +49(0) 541-969-2753
> D-49069 Osnabrueck                        Fax  : +49(0) 541-969-2745
> GERMANY
> ====================================================================
> 
> >  
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list