It seems like it would be easy to write yourself if it were just for vectors:
#include "Eigen/Core" template <typename T, typename T2> T extract(const T2& full, const T& ind) { int num_indices = ind.innerSize(); T target(num_indices); for (int i = 0; i < num_indices; i++) { target[i] = full[ind[i]]; } return target; } int main() { Eigen::VectorXd full(8); full << 1, 2, 3, 4, 5, 6, 7, 8; Eigen::Vector4d ind_vec(4); ind_vec << 0, 2, 4, 5; std::cout << "full:" << full<< std::endl; std::cout << "ind_vec:" << ind_vec<< std::endl; std::cout << "extracted" << extract(full,ind_vec) << std::endl; }
This should work in most cases.
edit: for cases where your scalar index type is different from your source and target scalar types, the following will work (for all built-in Eigen types).
template <typename T, typename T2> Eigen::Matrix<typename T2::Scalar,T::RowsAtCompileTime,T::ColsAtCompileTime,T::Options> extract2(const Eigen::DenseBase<T2>& full, const Eigen::DenseBase<T>& ind) { using target_t = Eigen::Matrix < T2::Scalar, T::RowsAtCompileTime, T::ColsAtCompileTime, T::Options > ; int num_indices = ind.innerSize(); target_t target(num_indices); for (int i = 0; i < num_indices; i++) { target[i] = full[ind[i]]; } return target; }
(this differs from the other in that you can use the ints vector as indices and the doubling vector as the source and get the doubling vector returned instead of the ints vector, as extract() )
source share