如何使用另一个"Eigen::VectorXd"向量中的值初始化"Eigen::VectorXd",基于索引的"向量<int>"

How to initialize an `Eigen::VectorXd` using values from another `Eigen::VectorXd` vector, based on a `vector<int>` of indexes

本文关键字:VectorXd 向量 Eigen 索引 lt int gt 初始化 另一个 何使用 于索引      更新时间:2023-10-16

我有兴趣使用另一个Eigen::VectorXd向量的值初始化Eigen::VectorXd向量,基于索引vector<int>,这些索引将指出将使用哪些元素。

那是

// This is the large vector from which I'll take the values to 
// initialize the second, shorter one
int N = 100;
Eigen::VectorXd V(N);
V.setRandom(N);
// This is the vector of indexes that'll be used to specify 
// which elements of V will be used in the initialization of x
vector<int> ids = {1, 3, 0, 20};
// This is the vector I want to initialize
Eigen::VectorXd x(ids.size());

现在,我希望x以下内容:

x(0) = V(1)

x(1) = V(3)

x(2) = V(0)

x(4) = V(20)

谢谢!

在 devel 分支(将变为 3.4)中,您可以执行以下操作:

x = V(ids);

在 Eigen 3.3 中,您必须编写自己的 for 循环。