[英]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. 我的意思是:我有一个2d矢量a和一个3d矢量b。 When I initialize b, b(0,1) should be set to a and b(2) should be set to 1. This code works: 当我初始化b时,b(0,1)应设置为a,b(2)应设置为1。此代码有效:
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
而在其他方面):
const Vector2d a(1,2);
const Vector3d b( a, 1 );
This doesn't work. 这行不通。 Is there a way to achieve this? 有没有办法做到这一点?
Without the const you would do: 如果没有const,您将执行以下操作:
Vector3d b;
b << a, 1;
If you really want it to be const, then you can do: 如果您确实希望将其设为const,则可以执行以下操作:
const Vector3d b = (Vector3d() << a, 1).finished();
Here's what you could do using a lambda (assuming there's not an easier way by using a constructor call): 这是您可以使用lambda进行的操作(假设没有使用构造函数调用的简单方法):
auto generateB = [&]() -> Vector3d {
Vector3d b(0, 0, 1);
b.head<2>() = a;
return b;
};
And then you'd use it like this: 然后您将像这样使用它:
auto const actualB = generateB();
Vector3d const anotherB = generateB();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.