如何访问C++ Eigen稀疏矩阵中的特定(row,col)索引?

Mat*_*lly 7 c++ indexing matrix sparse-matrix eigen

我在C++中使用Eigen中的稀疏矩阵.我想读取存储在特定行和列索引中的数据,就像我使用常规特征矩阵一样.

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
Run Code Online (Sandbox Code Playgroud)

我该如何进行这种索引操作?

Avi*_*urg 12

试试coeff:

double value = matrix.coeff(iRow, iCol);
Run Code Online (Sandbox Code Playgroud)

如果您想要使用非const版本coeffRef.请注意,使用时coeffRef如果元素不存在,则会插入该元素.