title: Randomly permute rows/columns of a matrix with eigen
body: <p>I'm using Eigen and I've got a matrix:</p>
  
  <pre><code>MatrixXi x = MatrixXi::Random(5);
  </code></pre>
  
  <p>I'd like to randomly permute the rows and columns using a randomly drawn permutation (just one permutation for both the rows and columns), i.e. if I have a permutation that sends indices [0,1,2,3,4] -> [3,4,2,1,0] than I want to reorder the rows and the columns with the same permutation. </p>
  
  <p>Part 1: I can't find an example of PermutationMatrix online, and I'm having trouble figuring out the syntax.</p>
  
  <p>Part 2: how do I obtain a randomly permuted vector of indices to pass to it? Maybe std::random_shuffle?</p>
  
  <p><strong>update:</strong></p>
  
  <p>Here is a (probably inefficient) way to get a shuffled set of indices:</p>
  
  <pre><code>std::vector&lt;int&gt; perm;
  for (int i=0; i&lt;5; ++i) {
      perm.push_back(i);
  }
  
  std::random_shuffle(perm.begin(), perm.end());
  </code></pre>
  
  <p>So now the question is how I do reorder my matrix x so that it's rows/columns are ordered by perm? </p>
  
  <p><strong>update 2:</strong></p>
  
  <p>Getting closer, this works (source for ideas: cplusplus.com):</p>
  
  <pre><code>int myrandom (int i) { return std::rand()%i;}
  
  PermutationMatrix&lt;Dynamic,Dynamic&gt; perm(5);
  
  perm.setIdentity();
  for (int i=dim-1; i&gt;0; --i) {
      swap (perm.indices()[i],perm.indices()[myrandom(i+1)]);
  }
  
  cout &lt;&lt; "original x" &lt;&lt; x &lt;&lt; endl &lt;&lt; endl;
  cout &lt;&lt; "permuted x" &lt;&lt; perm * x * perm &lt;&lt; endl &lt;&lt; endl;
  </code></pre>
  
  <p>Anyone know how to do this with random_shuffle? (see attempt that didn't work below.)</p>
  
  <p>(Bonus: any thoughts on whether perm * x * perm will be efficient if perm is a 1e4 x 1e4 matrix?)</p>