/************** Permute Matrix Entries ****************
Author: Alan G. Isaac
Based on suggestions by
  Mico Loretan mailto:loretanm@FRB.GOV
  David Baird  mailto:BairdD@AgResearch.CRI.NZ
  Ted Thompson mailto:tat5@cdc.gov
Dec 1996
Provided without guarantees for public non-commercial use.

Let x be any matrix that needs to be permuted randomly.
*************************************************************/
proc permute(x);
 local r,c,nobs,pidx;
 r=rows(x);
 c=cols(x);
 nobs = r*c;
 pidx = rankindx( rndu(nobs,1), 1);
 if c==1 or r==1;
  retp(x[pidx]);
 else;
  x = vecr(x);
  retp(reshape(x[pidx],r,c));
 endif;
endp;


/*
Examples:
To permute the numbers 1 to 20:
 permute(seqa(1,1,20));
To permute the rows of a matrix x:
 xp = x[permute(seqa(1,1,rows(x))),.];
*/
