gpt4 book ai didi

c++ - 如何根据行总计的条件在 Armadillo 中删除 SpMat 的行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:16 24 4
gpt4 key购买 nike

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

最佳答案

执行速度最快的解决方案很可能就是您提出的解决方案。如果您想利用高级 Armadillo 功能(即编码速度更快但运行速度可能更慢),您可以构建一个包含“坏”行 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);
}

关于c++ - 如何根据行总计的条件在 Armadillo 中删除 SpMat<unsigned int> 的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com