【Linux/Eigen】学习笔记(2):矩阵创建方法

在重构机器人算法时,Eigen库应该是使用率最多的库了,因为机器人的算法涉及到大量的矩阵运算。

首先说一下创建3×3的矩阵的方法,作为初学者我比较关心的是声明和赋值,用以下三种方式说明:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main()
{
    // 声明和赋值 方法1
    Eigen::Matrix3d mat3d1;
    mat3d1 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
    cout << "method1:" << endl;
    cout << mat3d1 << endl;
    // 声明和赋值 方法2
    Eigen::Matrix<double, 3, 3> mat3d2;
    mat3d2 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
    cout << "method2:" << endl;
    cout << mat3d2 << endl;
    // 声明和赋值 方法3
    Eigen::Matrix<double, 3, 3> mat3d3;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            mat3d3(i, j) = 100;
    cout << "method3:" << endl;
    cout << mat3d3 << endl;
    return 1;
}

上述代码的运行结果:

posted @ 2021-01-07 20:09  gpeng832  阅读(57)  评论(0)    收藏  举报  来源