特征稀疏矩阵得到非零元素指数

Nin*_*ina 2 sparse-matrix eigen

我是第一次使用Eigen Sparse Matrices,现在我想知道如何获得非零元素的索引.我构建了我的稀疏矩阵如下:

Eigen::SparseMatrix<Eigen::ColMajor> Am(3,3);
Run Code Online (Sandbox Code Playgroud)

我可以通过查看m_indices变量来查看VS中的一些索引.但我无法访问它们.谁能帮帮我吗?像矩阵一样

( 1 0 1 
  0 1 1
  0 0 0 )
Run Code Online (Sandbox Code Playgroud)

我希望指数是这样的(0,0), (0,2), (1,1), (1,2).

有什么办法吗?

PS我的矩阵大于3x3.

Avi*_*urg 6

教程的代码与此类似:

for (int k=0; k < A.outerSize(); ++k)
{
    for (SparseMatrix<int>::InnerIterator it(A,k); it; ++it)
    {
        std::cout << "(" << it.row() << ","; // row index
        std::cout << it.col() << ")\t"; // col index (here it is equal to k)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 找出为什么它给了我这个错误:我没有指定我想使用 RowMajor。使用 `SparseMatrix&lt;int, Eigen::RowMajor&gt;::InnerIterator` 反而起作用了! (2认同)