Home > Software engineering >  Forward and reverse conversions between vector<Eigen::Vector3f> and Eigen::Matrix3Xf
Forward and reverse conversions between vector<Eigen::Vector3f> and Eigen::Matrix3Xf

Time:07-27

How to convert vector<Eigen::Vector3f> to Eigen::Matrix3Xf ?

and

inverse operation ?

#include <Eigen/Eigen>
#include <iostream>
#include <vector>

using namespace std;

vector<Eigen::Vector3f> x{{1.0, 2.0, 3.0}, {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0}};

Eigen::Matrix3Xf y;

y = x?

vector<Eigen::Vector3f> x;

Eigen::Matrix3Xf y{{1.0, 2.0, 3.0}, {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0}};

x = y?

CodePudding user response:

Assignment vector to Matrix, use a Map (you need to make sure that x.size()>0):

// Works as constructor or assignment:
Eigen::Matrix3Xf y = Eigen::Matrix3Xf::Map(x[0].data(), 3, x.size());

The other way around, you can use the iterator interface of Eigen:

// Constructor:
vector<Eigen::Vector3f> x(y.colwise().begin(), y.colwise().end());
// Assignment:
x.assign(y.colwise().begin(), y.colwise().end());

Usually, there is no need to copy between both representations, you can use Eigen::Map directly in Eigen expressions or pass Eigen iterators directly to std algorithms.

  • Related