我在 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
如何执行这种类型的索引操作?
最佳答案
尝试 coeff
:
double value = matrix.coeff(iRow, iCol);
如果你想要一个非常量版本使用coeffRef
反而。请注意,当使用 coeffRef
时,如果元素不存在,它将被插入。
关于c++ - 如何访问 C++ Eigen 稀疏矩阵中的特定(行,列)索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42376127/