如何从Eigen中的索引向量中提取(Eigen :: Vector的)子向量? [英] How to extract a subvector (of an Eigen::Vector) from a vector of indices in Eigen?

查看:291
本文介绍了如何从Eigen中的索引向量中提取(Eigen :: Vector的)子向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

Eigen::VectorXd x; //{1,2,3,4,5,6,7,8}

Eigen::VectorXd ind_vec; //{0,2,4,5}

有没有一种简便的方法可以提取x的 ind_vec 个元素?

Is there a way an easy way to extract the ind_vec elements of x?

类似的东西:

x.extract(ind_vec) returning {1, 3, 5, 6}


推荐答案

由于当前答案对我而言并不令人满意,因此我在Google上进行了一些搜索,发现本教程在Eigen文档中。

Since the current answer was not satisfactory for me, I googled a bit and I found this tutorial in the Eigen documentation.

#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
  Eigen::ArrayXf v(6);
  v << 1, 2, 3, 4, 5, 6;
  cout << "v.head(3) =" << endl << v.head(3) << endl << endl;
  cout << "v.tail<3>() = " << endl << v.tail<3>() << endl << endl;
  v.segment(1,4) *= 2;
  cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl;
}

会输出:

v.head(3) =
1
2
3

v.tail<3>() = 
4
5
6

after 'v.segment(1,4) *= 2', v =
 1
 4
 6
 8
10
 6

I尚未使用向量进行测试,但我想也应该可行。

I haven't tested it with vectors, but I guess should be possible as well.

这篇关于如何从Eigen中的索引向量中提取(Eigen :: Vector的)子向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆