Search code examples
c++eigen

Eigen extracting submatrix from vector of indices


I have been googling for a while now, but cant find the answer to this simple question.

In matlab i can do this:

rows = [1 3 5 9];
A = rand(10);
B = A(rows, : );

How do i do this in eigen? It does not seem like it is possible. The closest thing i have found is

MatrixXd a(10,10);
a.row(1); 

,but I want to get multiple rows/cols. Another user has also asked the question here: How to extract a subvector (of a Eigen::Vector) from a vector of indices in Eigen? , but I think there must some built in way of doing this because it is a really common operation I think.

Thanks.


Solution

  • While this was not possible at the time this question was asked, it has since been added in the development branch!

    It's very straight forward:

    Eigen::MatrixXf matrix;
    Eigen::VectorXi columns;
    Eigen::MatrixXf extracted_cols = matrix(Eigen::all, columns);
    

    So I'm guessing this will be in the 3.3.5 3.4 stable release. Until then the development branch is the way to go.