s-news
[Top] [All Lists]

Re: permutation

To: <larisa.reyderman@spcorp.com>, <s-news@lists.biostat.wustl.edu>
Subject: Re: permutation
From: <Bill.Venables@csiro.au>
Date: Fri, 15 Apr 2005 08:11:45 +1000
Thread-index: AcVBNFllFpXd1hINS+6jcklxPBmyywACGsrg
Thread-topic: [S] permutation
Dear Larisa,
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Reyderman, Larisa
Sent: Friday, 15 April 2005 6:54 AM
To: 's-news@lists.biostat.wustl.edu'
Subject: [S] permutation

Dear All,

I have a seemingly easy question.  I need to create a matrix consisting of all possible permutations of the following vector:

0.5  1.0  1.5  2.0  3.0  4.0  5.0  6.0  8.0 10.0 12.0 16.0

I know that "sample" function with (replace=F) will generate a permutation, however, I need all possible permutations.
[WNV] You do realise that there are 12! = 479 001 600 permutations in all.  That's going to make a matrix with 12! x 12 = 5 748 019 200 entries in it, so I hope you did get that extra few Gb's of RAM...

If you have a more realistic case you can do this with a very simple function.  For example:

> permutations <- function(x)
  if (length(x) <= 1) as.matrix(x) else {
    M <- NULL
    for(i in 1:length(x)) 
      M <- rbind(M, cbind(x[i], Recall(x[-i])))
    M
  } 


[WNV]  As a check:

> permutations(1)
     [,1]
[1,]    1
> permutations(1:2)
     [,1] [,2]
[1,]    1    2
[2,]    2    1
> permutations(1:3)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    3    2
[3,]    2    1    3
[4,]    2    3    1
[5,]    3    1    2
[6,]    3    2    1

[WNV] Writing code that will generate all possible permutations sequentially is more difficult, of course, but not impossible.

Bill Venables. 

Thank you for your help.

Larisa



*********************************************************************
This message and any attachments are solely for the intended recipient. If you are not the intended recipient, disclosure, copying, use or distribution of the information included in this message is prohibited -- Please immediately and permanently delete.
<Prev in Thread] Current Thread [Next in Thread>