简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM