Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Created January 26, 2012 18:50
Show Gist options
  • Save tpoisot/1684333 to your computer and use it in GitHub Desktop.
Save tpoisot/1684333 to your computer and use it in GitHub Desktop.
Permutation of a matrix with the diagonal kept constant
mat = matrix(abs(round(rnorm(100,15,8),0))+1,10)
diag(mat) = 0
print(mat)
permat = function(mat)
{
mat[lower.tri(mat)+upper.tri(mat)==1] = sample(mat[lower.tri(mat)+upper.tri(mat)==1],replace=FALSE)
return(mat)
}
replicate(100,permat(mat))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment