I am using Eigen and I have a matrix:
MatrixXi x = MatrixXi::Random(5);
I would like to randomly rearrange rows and columns using an arbitrarily drawn permutation (only one permutation for rows and columns), i.e. if I have a permutation that sends indexes [0,1,2,3,4] β [3,4,2,1,0] than I want to reorder rows and columns with the same permutation.
Part 1: I cannot find an example of PermutationMatrix online, and it is difficult for me to understand the syntax.
Part 2: how do I get a randomly arranged index vector to go to it? Maybe std :: random_shuffle?
update:
Here is a (possibly inefficient) way to get a shuffled set of indices:
std::vector<int> perm; for (int i=0; i<5; ++i) { perm.push_back(i); } std::random_shuffle(perm.begin(), perm.end());
So now the question is, how do I reorder my matrix x so that rows / columns are sorted by perm?
update 2:
Closer, this works (source for ideas: cplusplus.com):
int myrandom (int i) { return std::rand()%i;} PermutationMatrix<Dynamic,Dynamic> perm(5); perm.setIdentity(); for (int i=dim-1; i>0; --i) { swap (perm.indices()[i],perm.indices()[myrandom(i+1)]); } cout << "original x" << x << endl << endl; cout << "permuted x" << perm * x * perm << endl << endl;
Does anyone know how to do this with random_shuffle? (see a try that doesn't work below.)
(Bonus: any thoughts on whether perm * x * perm will be effective if perm is a 1e4 x 1e4 matrix?)