Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen extracting submatrix from vector of indices

Tags:

c++

eigen

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.

like image 620
lijas Avatar asked Oct 16 '16 19:10

lijas


2 Answers

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.

like image 119
Daniel Hesslow Avatar answered Oct 13 '22 18:10

Daniel Hesslow


Unfortunately, this is still not directly supported even in Eigen 3.3. There has been this feature request for a while: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=329

Gael linked to an example implementation in one of the comments there: http://eigen.tuxfamily.org/dox-devel/TopicCustomizing_NullaryExpr.html#title1

like image 22
chtz Avatar answered Oct 13 '22 17:10

chtz