Eigen Sparse Matrix get Indices of Nonzero Elements

怎甘沉沦 提交于 2020-01-04 04:22:32

问题


I am using Eigen Sparse Matrices for the first time, and now I would like to know how to get the indices of the nonzero elements. I constructed my Sparse Matrix as follows:

Eigen::SparseMatrix<Eigen::ColMajor> Am(3,3);

and I can see some indices in VS by looking into the m_indices variable. But I can't access them. Can anyone please help me? For a Matrix like

( 1 0 1 
  0 1 1
  0 0 0 )

I would like the indices to be like (0,0), (0,2), (1,1), (1,2).

Is there any way to do it?

P.S. My matrices are way bigger than 3x3.


回答1:


The tutorial has code similar to this:

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)
    }
}



回答2:


Eigen::SparseMatrix<int, Eigen::ColMajor> A(2,3);
for (int k=0; k < A.outerSize(); ++k)
{
    for (Eigen::SparseMatrix<int,Eigen::ColMajor>::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)
    }
}


来源:https://stackoverflow.com/questions/28854640/eigen-sparse-matrix-get-indices-of-nonzero-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!