Search code examples
c++eigeneigen3

stacking vectors into a Eigen Matrix


I tried to make a Eigen matrix by stacking two vectors. To do that i created size 8 two vectors. Also i created a matrix. which is size of (2, 8). In order to do it i used following code. No compilation errors but also no output as well (m is seems empty) . Any help is appreciated. Thank you.

VectorXd v1;
v1 <<1,0,0,0,1,0,0,0;
VectorXd v2;
v2 << 0,1,0,0,0,1,0,0;

MatrixXd m(2, 8);
m.row(0) = v1;
m.row(1) = v2;

std::cout << m << std::endl;

Desired output is :

1,0,0,0,1,0,0,0
0,1,0,0,0,1,0,0

Solution

  • Your vectors should be initialized as follows for a (2, 8) matrix:

    VectorXd v1(8)
    VectorXd v2(8)
    

    Otherwise you will get a segmentation fault.