我创建了如下的犰狳c ++矩阵:
arma::mat A;
A.zeros(3,4);
我想将其转换为由定义的向量的向量
std::vector< std::vector<double> > B(3, std::vector<double>(4) );
如何将B设置为等于A?如果没有简单的矢量向量的方法,那么关于数组的数组呢,也就是说,如果我将B定义为
double B[3][4];
在这种情况下,您应该使用arma::conv_to
arma的一个完全精妙的功能。
请注意,此方法将要求源对象能够将其解释为向量。这就是为什么我们需要为每一行迭代地执行此操作。这是一种转换方法:
#include <armadillo>
typedef std::vector<double> stdvec;
typedef std::vector< std::vector<double> > stdvecvec;
stdvecvec mat_to_std_vec(arma::mat &A) {
stdvecvec V(A.n_rows);
for (size_t i = 0; i < A.n_rows; ++i) {
V[i] = arma::conv_to< stdvec >::from(A.row(i));
};
return V;
}
这是一个示例用法:
#include <iomanip>
#include <iostream>
int main(int argc, char **argv) {
arma::mat A = arma::randu<arma::mat>(5, 5);
std::cout << A << std::endl;
stdvecvec V = mat_to_std_vec(A);
for (size_t i = 0; i < V.size(); ++i) {
for (size_t j = 0; j < V[i].size(); ++j) {
std::cout << " "
<< std::fixed << std::setprecision(4) << V[i][j];
}
std::cout << std::endl;
}
return 0;
}
std::setprecision
用于生成更具可读性的输出:
0.8402 0.1976 0.4774 0.9162 0.0163
0.3944 0.3352 0.6289 0.6357 0.2429
0.7831 0.7682 0.3648 0.7173 0.1372
0.7984 0.2778 0.5134 0.1416 0.8042
0.9116 0.5540 0.9522 0.6070 0.1567
0.8402 0.1976 0.4774 0.9162 0.0163
0.3944 0.3352 0.6289 0.6357 0.2429
0.7831 0.7682 0.3648 0.7173 0.1372
0.7984 0.2778 0.5134 0.1416 0.8042
0.9116 0.5540 0.9522 0.6070 0.1567
祝你有个好的一天!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句