What is the method for assigning values to eigen matrices in C++?

In Eigen, matrices can be assigned values using bracket initialization, element-wise assignment, and block assignment.

  1. Initialize with parentheses:
Eigen::MatrixXf mat(3, 3); // 创建一个3x3的矩阵
mat << 1, 2, 3,
       4, 5, 6,
       7, 8, 9; // 使用“<<”运算符进行赋值
  1. Assign values to each element one by one.
Eigen::MatrixXf mat(3, 3); // 创建一个3x3的矩阵
mat(0, 0) = 1;
mat(0, 1) = 2;
mat(0, 2) = 3;
mat(1, 0) = 4;
mat(1, 1) = 5;
mat(1, 2) = 6;
mat(2, 0) = 7;
mat(2, 1) = 8;
mat(2, 2) = 9;
  1. initialize variables.
Eigen::MatrixXf mat(3, 3); // 创建一个3x3的矩阵
Eigen::MatrixXf block(2, 2); // 创建一个2x2的矩阵
block << 10, 20,
         30, 40;
mat.block(0, 0, 2, 2) = block; // 将block赋值给mat的一块区域

The above are three common ways of assignment, with initilization in brackets being more common and convenient in practical applications. Eigen also supports other more advanced ways of assignment, which can be found in detail in Eigen’s official documentation.

Leave a Reply 0

Your email address will not be published. Required fields are marked *