【发布时间】:2015-01-10 19:04:18
【问题描述】:
我想置换 Eigen 中稀疏矩阵的行和列。这是我的代码,但它不起作用。
#include <iostream>
#include <Eigen/Core>
#include <Eigen/SparseCore>
typedef Eigen::SparseMatrix<double> SpMat;
using namespace Eigen;
using namespace std;
int myrandom (int i) { return std::rand()%i;}
int main() {
PermutationMatrix<Dynamic,Dynamic> perm(5);
MatrixXd x = MatrixXd::Random(5,5);
SpMat y = x.sparseView();
int dim=5;
perm.setIdentity();
for (int i=dim-1; i>0; --i) {
swap (perm.indices()[i],perm.indices()[myrandom(i+1)]);
}
cout << "permutation\n" << perm.indices() << endl << endl;
cout << "original x\n" << y << endl << endl;
cout << "permuted left x \n" << perm * y << endl << endl;
cout << "permuted right x \n" << y * perm << endl << endl;
cout << "permuted both x \n" << perm * y * perm << endl << endl;
}
它置换行和置换列,但它不会同时进行。有谁知道如何排列列和行?
谢谢。
【问题讨论】:
标签: matrix permutation eigen sparse-matrix