置换特征矩阵的列

Permute Columns of Matrix in Eigen

我看了这个回答Randomly permute rows/columns of a matrix with eigen

但是他们将置换矩阵初始化为单位矩阵并进行随机洗牌。我想知道如何将矩阵初始化为特定的排列。

例如,如果我有一个整数向量,其中每个(索引、值)对表示我想将第 "index" 列移动到第 "value" 列,我该怎么做?

Eigen::MatrixXi M = Eigen::MatrixXi::Random(3,3);
std::vector<int> my_perm = {1,2,0};
some_function to return Matrix [M.col(1), M.col(2), M.col(0)]

编辑:dtell 在下面很好地回答了我原来的问题。

附加信息:

对于其他正在查看此内容的人 -- 如果您想用未知(编译时)数量的向量置换矩阵,您可以执行以下操作:

Eigen::VectorXi indices(A.cols());
for(long i = 0; i < indices.size(); ++i) {
    indices[i] = vector_of_indices[i];
}
Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> perm;
perm.indices() = indices;
Eigen::MatrixXd A_permute = A * perm; \ permute the columns

如果我没理解错的话,你问题的答案就是对你链接的答案稍作修改

Matrix3i A = Matrix3i::Random();
PermutationMatrix<3, 3> perm;
// Your permutation
perm.indices() = { 1, 2, 0 };
// Permutate rows
A = perm * A;