如何访问 C 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

我该如何执行这种类型的索引操作?

原文由 MattKelly 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 799
1 个回答

试试 coeff

 double value = matrix.coeff(iRow, iCol);

如果您想要一个非常量版本,请改用 coeffRef 。请注意,使用 coeffRef 时,如果该元素不存在,它将被插入。

原文由 Avi Ginsburg 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题