[R] permutations from vectors out of a matrix

Etienne Stockhausen einohr2002 at web.de
Wed Jan 20 19:31:51 CET 2010


Meyners,Michael,LAUSANNE,AppliedMathematics schrieb:
> Sorry, wrong button. Below a hopefully more helpful solution...
>
> Etienne,
> I don't see the point in avoiding some 'special' packages. If you are
> willing to change your mind in this regard, try one of the following
> solutions that work for me:
>
> library(combinat)
> apply(mat, 2, function(x) unique(permn(x)))
>
> # each object in the list contains the permutations from once column of
> mat:
> apply(mat, 2, function(x) do.call(rbind, unique(permn(x))))		
>
> # all vectors you wanted in one matrix; note that they are in the rows,
> so you might want to transpose this:
> do.call(rbind, apply(mat, 2, function(x) do.call(rbind,
> unique(permn(x)))))	
>
> Not sure about the size of your original problem, though, it might take
> a while. If you still want to avoid the (small!) package, you might
> consider copying the code for permn from combinat to define the function
> within your file. I guess it works (but didn't check) as it does not
> seem to require any of the other functions of the package.
>
> HTH, Michael
>
>
>   
>> -----Original Message-----
>> From: r-help-bounces at r-project.org 
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Etienne Stockhausen
>> Sent: Montag, 18. Januar 2010 19:20
>> To: r-help at r-project.org
>> Subject: [R] permutations from vectors out of a matrix
>>
>> Dear R-Users,
>> I'm relativley new to R and have the following problem. I 
>> need all permutations of the vectors created by the collumns 
>> of a matrix. I will give a small example:
>>    
>>     p=3
>>     
>> n=2^p-1                                                       
>>                   
>> #number of obtainable vectors
>>     mat=matrix(0,p,p)
>>     for(i in 1:p)
>>      {
>>        mat[i:1,i]=1/i
>>      }
>>
>> mat is now a quadratic matrix and n is the number of the 
>> vectors I try to get when I compute all permutations of the 
>> vectors built by the individual columns. It should work for 
>> all quadratic matrix and I want to avoid using some 'special' 
>> packages.
>> In the example I need the following vectors at the end:
>> (1,0,0); (0,1,0); (0,0,1); (0.5,0.5,0); (0.5,0,0.5); 
>> (0,0.5,0.5); (1/3,1/3,1/3).
>> I hope my intention becomes clear.
>>
>> I'm looking foward to any ideas and clues that might help me.
>> Thanks in advance and best regards.
>>
>> Etienne
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>     
>> ------------------------------------------------------------------------
>>
>>
>> Eingehende eMail ist virenfrei.
>> Von AVG uberpruft - www.avg.de 
>> Version: 9.0.730 / Virendatenbank: 270.14.150/2632 - Ausgabedatum: 01/19/10 08:34:00 
>>
>>     
Hey Michael,

thanks a lot for your answer. I hope I can manage it now. The problem 
with the 'small' package is, that I have to solve the task without using 
any package as a 'black box'. Therefore I need to get the permutations 
on my own.

Best regards and thanks again

Etienne



More information about the R-help mailing list