c++ - How do I convert an armadillo matrix to a vector of vectors? -
i created armadillo c++ matrix follows:
arma::mat a; a.zeros(3,4);
i want convert vector of vectors defined
std::vector< std::vector<double> > b(3, std::vector<double>(4) );
how set b equal a? if there not easy way vector of vectors, array of arrays, i.e., if defined b
double b[3][4];
in such cases should use arma::conv_to
totally superb feature of arma.
note method require source object able interpreted vector. why need iteratively every row. here conversion method:
#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); (size_t = 0; < a.n_rows; ++i) { v[i] = arma::conv_to< stdvec >::from(a.row(i)); }; return v; }
and here exemplary usage:
#include <iomanip> #include <iostream> int main(int argc, char **argv) { arma::mat = arma::randu<arma::mat>(5, 5); std::cout << << std::endl; stdvecvec v = mat_to_std_vec(a); (size_t = 0; < v.size(); ++i) { (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
used generate more readable output:
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
have one!
Comments
Post a Comment