Eigen矩阵常用方法

Eigen

矩阵运算

点乘&对列求和

method

    cout << m_a.array() * m_a.array() << endl; //method 1
    cout << m_a.cwiseProduct(m_a) << endl;     //method 2

example

    Matrix3d m_a;
    m_a << 2, -1, 0, -1, 2, -1, 0, 2, 2;
    
    cout << m_a.array() * m_a.array() << endl; //method 1
    cout << m_a.cwiseProduct(m_a) << endl;     //method 2

    cout << m_a.cwiseProduct(m_a).colwise().sum() << endl; //sum by column
    /*  output
    4 1 0
	1 4 1
	0 4 4 

	5 9 5*/

broadcast

method

    arr.rowwise()+=vec_row; //arr shape: (2, 4)   vec_row shape: (1, 4)
    arr.colwise()+=vec_col; //arr shape: (2, 4)   vec_col shape: (2, 1)

exampel

    // declare
    Matrix<double, 2, 4> arr = Matrix<double, 2, 4>::Zero();
    Matrix<double,1,4> vec_row;
    Matrix<double, 2, 1> vec_col; // equals to     Vector2d vec_col;

    // assign
    arr << 1, 2, 6, 9,
           3, 1, 7, 2;
    vec_row << 0, 1, 0, 1;
    vec_col << 0,
               1;
    //  broadcast by row
    arr.rowwise()+=vec_row;
    cout << "matrix:\n" << arr << endl;
    //  broadcast by col
    arr.colwise()+=vec_col;
    cout << "matrix:\n" << arr << endl;
    /* output
	matrix:
	 1  3  6 10
	 3  2  7  3
	matrix:
	 1  3  6 10
	 4  3  8  4 */

concatenate

method

新初始化一个大小是拼接后的矩阵, 然后直接使用<< 即可

example

void test_concat(){
    Eigen::Matrix2d A;
    Eigen::Matrix2d B;
    A << 1, 2,
         3, 4;
    B << 5, 6,
         7, 8;

    //horizontally
    Eigen::MatrixXd C(A.rows(), A.cols()+B.cols());
    C << A, B;
    cout << "matrix C: \n" << C << endl;

    //vertically
    Eigen::MatrixXd D(A.rows()+B.rows(), A.cols());
    D << A, B;
    cout << endl << "matrix D: \n" << D << endl;
}

/* output 
matrix C: 
1 2 5 6
3 4 7 8

matrix D: 
1 2
3 4
5 6
7 8 */

reshape

method

#method 1. m.reshaped(2, 8); //2-dimension
#method 2. m.reshaped().transpose();   //行优先
#method 3. m.reshaped<RowMajor>().transpose(); //列元素优先

*example

    measurement << 1, 2, 1, 1, 
                    6, 9, 1, 1, 
                    3, 1, 1, 1, 
                    7, 2, 1, 1;
    cout << measurement.reshaped(2, 8) << endl; //method 1
    /*output 1 
    1 3 2 1 1 1 1 1
	6 7 9 2 1 1 1 1*/
	cout << measurement.reshaped().transpose() << endl; //method 2
	/* output 2
	1 6 3 7 2 9 1 2 1 1 1 1 1 1 1 1*/
	cout << measurement.reshaped<RowMajor>().transpose() << endl; //method 3
	/* output 3
	reshaped<RowMajor>().transpose()*/
		

block operation

method

#method 1.  bbox2.block<bbox2.rows(), 2>(0, 0); //动态矩阵会报错 <> 中是block size,  ()中是开始的位置
#method 2.  bbox2.block(0, 0, bbox2.rows(), 2); //动态矩阵不会报错 ()依次是开始位置, 块大小

example

    Eigen::Matrix4d bbox1;
    bbox1 << 577,         403,          53,         127,
            29,         298,          40,         121,
            492,         159,          39,         100,
            560,         303,          52,         110;

    Eigen::Matrix<double, 4, 4> bbox;   
    bbox << 577,         403,          53,         127,
            29,         298,          40,         121,
            492,         159,          39,         100,
            560,         303,          52,         110;
    
    Eigen::MatrixX4d bbox2 = bbox;   //这种类型使用下行的block operation会报错,但是使用下下行不会报错
    // bbox2.block<bbox2.rows(), 2>(0, 0);    报错(应该是因为他的类型是动态矩阵,因为bbox1这么用并没有报错)
    // bbox2.block(0, 0, bbox2.rows(), 2);  不会报错

    //将bbox1从索引(0, 2)开始的1x2大小的block 加上从索引(0, 0)开始的1x2大小的block
    bbox1.block<1, 2>(0, 2) += bbox1.block<1, 2>(0, 0); //从(0,2)开始的1行两列元素的大小
    cout << "bbox1:\n" << bbox1 << endl << endl;

    //将bbox2从索引(0, 2)开始的2列大小的block 加上从索引(0, 0)开始的2列大小的block
    // bbox2.block<bbox2.rows(), 2>(0, 2) += bbox2.block<bbox2.rows(), 2>(0, 0); //会报错
    bbox2.block(0, 2, bbox2.rows(), 2) += bbox2.block(0, 0, bbox2.rows(), 2);    //不会报错
    cout << "bbox2:\n" << bbox2 << endl;

solve linear equation

更多求解方法以及要求,精度信息可以参考这个链接

    Matrix3d A;
    A << 3, 1, 1, 0, 3, 1, 1, 0, 3;

    Eigen::RowVector3d a = Eigen::RowVector3d::Ones(1, 3);

    A.rowwise() -= a;   //broadcast by row
    Eigen::Matrix<double, 3, 2> b;
    b << 4, 2, 0, 3, 5, 4;

    //solve:Ax = b, the answer is:
        // 2 1
        // 1 2
        // 3 3
        
	//positive definite matrix is required, more info. see the link above
    MatrixXd x = (A.transpose()*A).llt().solve(A.transpose()*b); 


    // cout << x.rows() << "-" << x.cols() << endl;
    cout << "\n\nA(shape is (" + std::to_string(A.rows()) + 'x' + std::to_string(A.cols()) + ")):\n" << A << endl;
    cout << "\n\nb(shape is (" + std::to_string(b.rows()) + 'x' + std::to_string(b.cols()) + ")):\n" << b << endl;
    cout << "\n\nx(shape is (" + std::to_string(x.rows()) + 'x' + std::to_string(x.cols()) + ")):\n" << x << endl;
    
    /* output
   	A(shape is (3x3)):
	 2  0  0
	-1  2  0
	 0 -1  2
	
	
	b(shape is (3x2)):
	4 2
	0 3
	5 4
	
	
	x(shape is (3x2)):
	2 1
	1 2
	3 3*/
    ```
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值