是否有一种有效的方法只保留Armadillo稀疏矩阵的行,这些矩阵总计至少矩阵各列的总计数水平?例如,如果其值的总和为i
,我希望保留>=C
行,其中C
是某个选定值。 Armadillo的文档说,稀疏矩阵只允许连续的子矩阵视图。所以我猜这是不容易通过子设置获得的。是否有替代方法可以明确地循环遍历元素并使用符合所需条件的新位置,值和colPtr设置创建新的稀疏矩阵?谢谢!
答案 0 :(得分:1)
最快的执行解决方案可能就是您建议的解决方案。如果你想利用高级犰狳功能(即编写速度更快但运行速度更慢),你可以构建一个std::vector
“坏”行ID,然后使用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);
}