I'm working in C++ with a sparse matrix in Eigen. I would like to read the data stored in a specific row and column index just like I would with a regular eigen matrix.
std::vector<Eigen::Triplet<double>> tripletList;
// TODO: populate triplet list with non-zero entries of matrix
Eigen::SparseMatrix<double> matrix(nRows, nCols);
matrix.setFromTriplets(tripletList.begin(), tripletList.end());
// TODO: set iRow and iCol to be valid indices.
// How to read the value at a specific row and column index?
// double value = matrix(iRow, iCol); // Compiler error
How do I go about performing this type of indexing operation?
Try coeff
:
double value = matrix.coeff(iRow, iCol);
If you want a non-const version use coeffRef
instead. Note that when using coeffRef
if the element doesn't exist, it will be inserted.
This Code work for me
for (int i=0; i<matrix.rows(); ++i){
for(int j=0; i<matrix.cols(); ++j)
cout << " i,j=" << i << ","
<< j << " value="
<< matrix.coeff(i,j)
<< std::endl;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With