当前位置: 代码迷 >> 综合 >> Eigen使用笔记——矩阵初始化
  详细解决方案

Eigen使用笔记——矩阵初始化

热度:74   发布时间:2024-03-08 18:04:56.0

1.直接输入

Matrix3f m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;

2.初始化为ones,zeros,Identity

MatrixXd m = MatrixXd::Zero(col,row);
MatrixXd m = MatrixXd::Ones(col,row);
MatrixXd m = MatrixXd::Identity(col,row);

3.使用矩阵来给另一个矩阵赋值

MatrixXf matA(2, 2);
matA << 1, 2, 3, 4;
MatrixXf matB(4, 4);
matB << matA, matA/10, matA/10, matA;
std::cout << matB << std::endl;
//output
1 2 0.1 0.2
3 4 0.3 0.4
0.1 0.2 1 2
0.3 0.4 3 4

或者使用block,row,col等

Matrix3f m;
m.row(0) << 1, 2, 3;
m.block(1,0,2,2) << 4, 5, 7, 8;
m.col(2).tail(2) << 6, 9;
std::cout << m;
//output
1 2 3
4 5 6
7 8 9