Search code examples
c++eigen

How can I initialize an "Eigen" - Matrix blockwise?


Sorry for the subject - I wasn't able to create a better one...

What I mean is: I have a 2d-Vector a and a 3d-Vector b. When I initialize b, b(0,1) should be set to a and b(2) should be set to 1. This code works:

    const Vector2d a(1,2);
    Vector3d b( 0,0,1 );
    b.head<2>() = a;

But what I would rather have (among others because of the const):

    const Vector2d a(1,2);
    const Vector3d b( a, 1 );

This doesn't work. Is there a way to achieve this?


Solution

  • Without the const you would do:

    Vector3d b;
    b << a, 1;
    

    If you really want it to be const, then you can do:

    const Vector3d b = (Vector3d() << a, 1).finished();