【问题标题】:How to initialize an `Eigen::VectorXd` using values from another `Eigen::VectorXd` vector, based on a `vector<int>` of indexes如何根据索引的`vector<int>`使用来自另一个`Eigen::VectorXd`向量的值初始化`Eigen::VectorXd`
【发布时间】:2017-01-26 01:50:13
【问题描述】:

我有兴趣使用来自另一个 Eigen::VectorXd 向量的值来初始化一个 Eigen::VectorXd 向量,基于索引的 vector&lt;int&gt; 将指出将使用哪些元素。

也就是说,

// 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)

非常感谢!

【问题讨论】:

    标签: c++ vector initialization eigen


    【解决方案1】:

    在 devel 分支(将变为 3.4)你可以这样做:

    x = V(ids);
    

    使用 Eigen 3.3,您必须编写自己的 for 循环。

    【讨论】:

    • 非常感谢 :) 顺便说一句,你知道什么时候可以使用吗?
    • 让我们期待春天的结束
    • 不错!谢谢你的回答:)
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2023-01-29
    • 2017-03-22
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2017-11-01
    相关资源
    最近更新 更多