Eigen知识点1:数组、向量初始化

1 知识点总结:

(1)数组初始化

 Eigen::MatrixXd m(2,2);
 m(0,0)=1;
m<<1,2,3,4;
MatrixXd m=MatrixXd::Random(3,3);
 MatrixXd m1=MatrixXd::Constant(3,3,1.2);
 MatrixXd m2=Matrix2d::Zero();  //零初始化
 MatrixXd m3=Matrix3d::Ones(); //初始化为1
 MatrixXf mat=MatrixXf::Ones(2,3);
 MatrixXd m4=Matrix4d::Identity(); //初始化为单位矩阵

(2)向量初始化

VectorXd v1(3);
 v1<<4,5,6;
RowVectorXd v(3);
 v<<1,2,3; //逗号初始化

(3)调整矩阵大小

MatrixXd m(2,5);
m.resize(4,5);
VectorXd v(2);
cout<<"v="<<v.size()<<" "<<v.rows()<<" "<<v.cols()<<endl;
v.resize(5);

2 eigen01.cpp

#include"ros/ros.h"
#include
#include
using namespace Eigen;
using namespace std;
int main(int argc, char*argv[])
{
    ros::init(argc,argv,"eigen01");
    ros::NodeHandle nh;
    //Eigen提供的初始化方法——逗号初始化法

    // 知识点1:数组初始化
    // 数组初始化方法1-1:直接赋初值
    // // MatrixXd表示动态数组,初始化的时候指定数组的行数和列数
    // // Eigen::MatrixXd m(2,2);
    // MatrixXd m(2,2);
    // // //  m(i,j);表示第i行第j列的值,这里对数组进行初始化
    // // m(0,0)=1;
    // // m(0,1)=1;
    // // m(1,0)=1;
    // // m(1,1)=1;
    // // m(1,1)=m(0,1)+m(1,0);
    // //  数组初始化方法1-2:或者采用逗号初始化法
    // m<<1,2,3,4;
    //  std::cout<

    //数组初始化方法2:
    // // 初始化动态矩阵——用随机数(函数)赋初始,初始化的值在[-1,1]区间内
    // MatrixXd m=MatrixXd::Random(3,3);
    // cout<<"m="<
    // //MatrixXd::Constant初始化矩阵三行三列,矩阵里边的数值均为1.2
    // MatrixXd m1=MatrixXd::Constant(3,3,1.2);
    // cout<<"m1="<
    // m=(m+m1)*50;
    // cout<<"m="<
    // MatrixXd m2=Matrix2d::Zero();  //零初始化
    // cout<<"m2="<
    // MatrixXd m3=Matrix3d::Ones(); //初始化为1
    // cout<<"m3="<
    //  MatrixXd m4=Matrix4d::Identity(); //初始化为单位矩阵
    // cout<<"m4="<
    // MatrixXf mat=MatrixXf::Ones(2,3);
    // cout<<"mat="<
    // //此处使用了临时变量,然后使用了逗号初始化,此处必须使用.finished()来获取矩阵对象
    // cout<<"after="<< (MatrixXf(2,2)<<1,2,3,4).finished()*mat;
   



    // 知识点2:向量初始化——三行一列[1,2,3]T
    //  方法2-1:VectorXd默认为一列
    // VectorXd v(3); //默认写的是一列
    // v<<1,2,3; //逗号初始化
    // cout<<"v="<
    // cout<<"m*v="<
    // VectorXd v1(3);
    // v1<<4,5,6;
    // VectorXd v2(6);
    // v2<
    // cout<<"v2="<


    // // 方法2-2:RowVectorXd默认为1行
    // RowVectorXd v(3);
    // v<<1,2,3; //逗号初始化
    // cout<<"v="<
    // // cout<<"m*v="<
    // RowVectorXd v1(3);
    // v1<<4,5,6;
    //  RowVectorXd v2(6);
    // v2<
    // cout<<"v2="<
    
// 知识点3:调整矩阵大小
MatrixXd m(2,5);
// 返回行数与列数
cout<<m.rows()<<m.cols()<<endl;
m.resize(4,5);
cout<<m.rows()<<m.cols()<<endl;
// 返回矩阵大小(系数)
cout<<m.size()<<endl;
//调整vector大小
VectorXd v(2);
cout<<"v="<<v.size()<<" "<<v.rows()<<" "<<v.cols()<<endl;
v.resize(5);
cout<<"v="<<v.size()<<" "<<v.rows()<<" "<<v.cols()<<endl;
    return 0;
}

你可能感兴趣的:(Eigen,c++,矩阵,开发语言)