Question

I was trying to iterate over the non zero elements of a row major sparse matrix, such as shown below:

Eigen::SparseMatrix<double,Eigen::RowMajor> Test(2, 3);
Test.insert(0, 1) = 34;
Test.insert(1, 2) = 56;
for (int k = 0; k < Test.outerSize(); ++k){
    for (Eigen::SparseMatrix<double>::InnerIterator it(Test, k); it; ++it){
        cout << it.row() <<"\t";
        cout << it.col() << "\t";
        cout << it.value() << endl;
    }
}

but I dont see the right values. Instead, I see random values for it.row(), a value of 1 for it.col() and some random value for it.value(), as shown below:

-17891602       1       -2.65698e+303

Changing RowMajor to ColumnMajor makes the code work as expected.

I am not sure what went wrong for the row major part ? Can someone please let me know what am I missing here ?

Thanks in advance

Was it helpful?

Solution

I'm surprised that it compiles fine: the type of your iterator is not correct. It must be a SparseMatrix<double,Eigen::RowMajor>::InnerIterator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top