如何在 c++/犰狳中去除不连续的索引
How to shed non-contiguous indices in c++/armadillo
我正在寻找一种干净的方法来摆脱使用犰狳线性代数库进行C++的非连续索引。我在下面包含了一些代码,但似乎可能有更好的方法。任何建议表示赞赏。
以下代码用于从(列(向量a
中删除ind
中的索引,但感觉很笨拙。
for(uword k = ind.n_elem; k>0; k--){
a.shed_row(ind(k-1));
}
有什么想法吗?
以下是使用模板化函数根据要排除的索引的(排序(uvec
删除行的一种方法。您从std::set_difference
获取缺少的索引,然后从那里开始。
#define ARMA_USE_CXX11
#include <armadillo>
#include <iostream>
template <class T>
T drop_rows(T a, arma::uvec exclude) {
arma::uvec full_range = arma::regspace<arma::uvec>(0, a.n_rows - 1);
std::vector<int> diff;
std::set_difference(full_range.begin(), full_range.end(),
exclude.begin(), exclude.end(),
std::inserter(diff, diff.begin()));
T b = a.rows(arma::conv_to<arma::uvec>::from(diff));
return b;
}
int main() {
arma::uvec exclude = {0, 1, 4};
arma::vec a = arma::linspace<arma::vec>(100, 500, 5);
arma::vec b = drop_rows(a, exclude);
std::cout << b << std::endl;
arma::mat A = arma::mat(5, 5, arma::fill::eye);
arma::mat B = drop_rows(A, exclude);
std::cout << B << std::endl;
return 0;
}
相关文章:
- 向量的内存位置不连续
- 有没有办法将不连续的捕获组组合到单个正则表达式中?
- 如何删除 Eigen3 矩阵中某些不连续的行和列?
- 二叉搜索功能始终返回 -1 值而不是索引
- 如何在 c++/犰狳中去除不连续的索引
- 如何在不重新索引顶点的情况下调用"boost::remove_vertex"?
- 地址在多个指针中不连续
- OpenCV 错误:图像步长错误(矩阵不连续)
- 如何从特征矩阵中获取不连续的数据"block"?
- 递归类型真的是构建不连续的任意大小数据结构的唯一方法吗
- 使用linux功能读取块的不连续运行
- 为什么使用 push_back() 赋值时向量元素的地址不连续
- ctags 不是索引类成员函数
- SIMD 指令中不接受索引
- 如何将可变数量的不连续缓冲区传递给内核
- 在不丢失索引信息的情况下对std::vector进行排序
- 为什么我的windows服务不连续写入文件
- 选择Rcpp中的不连续子矩阵
- gluLookAt中的不连续
- 在c++中填充数组而不翻译索引