如何根据行总数的条件在 Armadillo 中删除 SpMat<unsigned int> 的行?

How to drop rows of an SpMat<unsigned int> in Armadillo based on a condition on row totals?

是否有一种有效的方法可以只保留犰狳稀疏矩阵的行,这些行总和至少达到矩阵各列的总计数水平?例如,如果其值的总和为 >=C,我想保留第 i 行,其中 C 是某个选定的值。 Armadillo 的文档说稀疏矩阵只允许连续的子矩阵视图。所以我猜这不容易通过子设置获得。除了简单地遍历元素并创建一个具有新位置、值和 colPtr 设置以匹配所需条件的新稀疏矩阵之外,是否有替代方法?谢谢!

执行速度最快的解决方案很可能就是您提出的解决方案。如果您想利用高级犰狳功能(即编码速度更快但 运行 可能更慢),您可以构建 "bad" 行 ID 的 std::vector 然后使用 shed_row(id).删除行时注意索引。这是通过始终从矩阵底部脱落来实现的。

auto mat = arma::sp_mat(rowind, colptr, values, n_rows, n_cols)
auto threshold_value = 0.01 * arma::accu(sp_mat); // Sum of all elements

std::vector<arma::uword> bad_ids; // The rows that we want to shed 
auto row_sums = arma::sum(mat); // Row sums
// Iterate over rows in reverse order.
for (const arma::uword row_id = mat.nrows; i-- > 0; ) {
  if (row_sum(row_id) < threshold_value) {
    bad_ids.push_back(row_id);
  }
}
// Shed the bad rows from the bottom of the matrix and up.
for (const auto &bad_id : bad_ids) { 
  matrix.shed_row(bad_id);
}