Skip to content
Snippets Groups Projects
Commit 55862aa0 authored by Saad Jbabdi's avatar Saad Jbabdi
Browse files

added a permutation function

parent 37678373
No related branches found
No related tags found
No related merge requests found
......@@ -145,4 +145,33 @@ ReturnMatrix mvnrnd(const RowVector& mu, const SymmetricMatrix& covar, int nsamp
return mvn.next(nsamp);
}
ReturnMatrix perms(const int n){
if(n<=1){
Matrix P(1,1);
P << n;
P.Release();
return P;
}
Matrix Q = perms(n-1); // recursive calls
int m = Q.Nrows();
Matrix P(n*m,n);
for(int i=1;i<=m;i++){
P(i,1)=n;
for(int j=1;j<=Q.Ncols();j++)
P(i,j+1)=Q(i,j);
}
for(int i=n-1;i>=1;i--){
int jj=1;
for(int j=(n-i)*m+1;j<=(n-i+1)*m;j++){
P(j,1)=i;
for(int k=1;k<=n-1;k++){
P(j,k+1)= (Q(jj,k)==i) ? n : Q(jj,k);
}
jj++;
}
}
P.Release();
return P;
}
}
......@@ -41,6 +41,9 @@ namespace MISCMATHS {
ReturnMatrix gammacdf(const RowVector& vals, const float mu = 0, const float var = 1);
// returns n! * n matrix of all possible permutations
ReturnMatrix perms(const int n);
class Mvnormrandm
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment