我认为 Eigen 使用压缩方法来存储稀疏矩阵。有什么方法可以从 std::vectors 中提取特征稀疏矩阵的三重格式向量?
Thanks.
更多信息(三元组格式的示例)
矩阵的三元组格式:
A=
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
i = 1 1 2 3 3 4 // row
j = 1 3 3 2 4 1 // column
S = 3 4 1 2 5 4 // values
问题的答案是:
// Is there some method such as:
std::vector<Eigen::Triplet<double>> T = SparseMat.to_triplets();
// in Eigen?
不是,好像没有这样的功能。
反而,
std::vector<Eigen::Triplet<double>> to_triplets(Eigen::SparseMatrix<double> & M){
std::vector<Eigen::Triplet<double>> v;
for(int i = 0; i < M.outerSize(); i++)
for(typename Eigen::SparseMatrix<double>::InnerIterator it(M,i); it; ++it)
v.emplace_back(it.row(),it.col(),it.value());
return v;
}
auto t = to_triplets(SparseMat);
如果你想做得更快,可以在 IDE 中打开它,查找指向数据数组的指针,然后编写一个对运行时没有影响的复杂函数,因为矩阵是稀疏的,并且复制在以下方面是线性的:非零元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)