c++ - 如何访问 C++ Eigen 稀疏矩阵中的特定(行,列)索引?

标签 c++ matrix indexing 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

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

最佳答案

尝试 coeff :

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

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

关于c++ - 如何访问 C++ Eigen 稀疏矩阵中的特定(行,列)索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42376127/

相关文章:

c++ - 在 C++ 中继承 C 结构并将其传递回 C 库是否安全?

c++ - 如何在 opencv 中将 QPointf 转换为 Point?

r - 对 R 中的嵌套 for 循环进行向量化(用于多类逻辑损失计算)

sql - 索引如何减慢 select 语句的速度?

MySQL显示查询中使用的索引

c++ - "<address>"处的指令引用了 "<address>"处的内存。内存不能是 "read."

c++ - 如何在编译时检查表达式是否非法?

矩阵的行积与矩阵的列和

matlab - 如何对具有相同列值的行进行分组?

indexing - 使用 Redis 和 Sinatra 为 Facebook 应用程序建立地理空间索引