John Zhang
John Zhang

Reputation: 23

eigen(c++): how can I visit part of matrix in?

In matlab, I can use A(1:5,2:4)to visit elements of 1-5 row &2-4 columns in a matrix. In c++ eigen library, how i can visit part of the elements like in matlab?

Upvotes: 2

Views: 198

Answers (1)

JHBonarius
JHBonarius

Reputation: 11271

Use the block function

#include <Eigen/Dense>
#include <iostream>
 
int main()
{
  Eigen::MatrixXf m(4,4);
  m <<  1, 2, 3, 4,
        5, 6, 7, 8,
        9,10,11,12,
       13,14,15,16;
  std::cout << "m(2:3, 2:3) = \n";
  std::cout << m.block<2,2>(1,1) << '\n';
}

returns

m(2:3, 2:3) = 
 6  7
10 11

You can even write to the matrix that way! See the link above.

Upvotes: 6

Related Questions