Eigen
矩阵运算
点乘&对列求和
method
复制代码
1
2
3cout << m_a.array() * m_a.array() << endl; //method 1 cout << m_a.cwiseProduct(m_a) << endl; //method 2
example
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14Matrix3d 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
复制代码
1
2
3arr.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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25// 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
复制代码
1
2新初始化一个大小是拼接后的矩阵, 然后直接使用<< 即可
example
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30void 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
复制代码
1
2
3
4#method 1. m.reshaped(2, 8); //2-dimension #method 2. m.reshaped().transpose(); //行优先 #method 3. m.reshaped<RowMajor>().transpose(); //列元素优先
*example
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16measurement << 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
复制代码
1
2
3#method 1. bbox2.block<bbox2.rows(), 2>(0, 0); //动态矩阵会报错 <> 中是block size, ()中是开始的位置 #method 2. bbox2.block(0, 0, bbox2.rows(), 2); //动态矩阵不会报错 ()依次是开始位置, 块大小
example
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25Eigen::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
更多求解方法以及要求,精度信息可以参考这个链接
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42Matrix3d 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 << "nnA(shape is (" + std::to_string(A.rows()) + 'x' + std::to_string(A.cols()) + ")):n" << A << endl; cout << "nnb(shape is (" + std::to_string(b.rows()) + 'x' + std::to_string(b.cols()) + ")):n" << b << endl; cout << "nnx(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*/ ```
最后
以上就是忐忑大碗最近收集整理的关于Eigen矩阵常用方法Eigen的全部内容,更多相关Eigen矩阵常用方法Eigen内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复