Ting
Ting

Reputation: 83

Iterating over symmetric sparse matrix in Eigen

I am following the following example to loop through the elements of a sparse matrix.

SparseMatrix<double> mat(rows,cols);
for (int k=0; k<mat.outerSize(); ++k)
  for (SparseMatrix<double>::InnerIterator it(mat,k); it; ++it)
  {
    it.value();
    it.row();   // row index
    it.col();   // col index (here it is equal to k)
    it.index(); // inner index, here it is equal to it.row()
  }

I am completely fine with this example. However, the matrix I have is symmetric and I only want to iterator through the lower part. Is there any easy way to iterate through a symmetric matrix?

Upvotes: 4

Views: 1256

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

You could check for it at the beginning of each loop:

for (int k=0; k<mat.outerSize(); ++k)
  for (SparseMatrix<double>::InnerIterator it(mat,k); it; ++it)
  {
      if(it.row() < it.col())
          continue;
  }

Upvotes: 1

Related Questions