Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert STL vector of vector to armadillo mat?

Tags:

c++

stl

armadillo

Given vector<vector<double > > A_STL, I want it to get converted into arma::mat A.

like image 337
Palash Kumar Avatar asked Jun 02 '15 09:06

Palash Kumar


1 Answers

One simple way would be to flatten your vector of vector matrix into one dimensional vector. And you can therefore use your mat(std::vector) constructor.

Code example (not tested) :

// Flatten your A_STL into A_flat
std::vector<double> A_flat;
for (auto vec : A_STL) {
  for (auto el : vec) {
    A_flat.push_back(el);
  }
}
// Create your Armadillo matrix A
mat A(A_flat);

Beware of how you order your vector of vector. A_flat should be column major.

like image 115
coincoin Avatar answered Oct 27 '22 14:10

coincoin