Search code examples
c++eigen

Un-flatten Eigen::VectorXd to Eigen::MatrixXd


Let's say I have a 6-dimensional Eigen::VectorXd

Eigen::VectorXd flat;
flat.resize(6);
flat << 1,2,3,4,5,6;

I want to un-flatten this into a 2x3-dimensional Eigen::MatrixXd

1,2,3,
4,5,6

How is this done most efficiently?


Solution

  • Not sure about efficiency, but this would work:

    Eigen::Map<Eigen::MatrixXd> M(flat.data(), 3, 2);
    Eigen::MatrixXd M2(M.transpose());
    

    Your matrix M2 is the desired matrix.