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.