Search code examples
matlabpermutation

Random permutation matrix


Is there an easy way to simulate a random permutation matrix (say of size 1000 by 1000) in Matlab? I would like to study the eigenvalue distribution of independent sum of such matrices.

Thanks in advance!


Solution

  • You can generate a random permutation matrix like so:

    1. Create a unity matrix:

      A = eye( N );  %// N is the size of your matrix
      

      For large values of N it is better to use sparse matrices:

      A = speye( N ); % create sparse identity matrix
      
    2. Generate a random permutation:

      idx = randperm(1:N);
      
    3. Use vector indexing to rearrange the rows accordingly

      A = A(idx, :);
      

    Voila!