1. 块操作
块是matrix或array中的矩形子部分。
函数.block(),有两种形式
Eigen中,索引从0开始。
两个版本都可以用于固定尺寸和动态尺寸的matrix/array。功能是等价的,只是固定尺寸的版本在block较小时速度更快一些。
1 int main()
2
3 {
4
5 Eigen::MatrixXf m(4,4);
6
7 m << 1, 2, 3, 4,
8 5, 6, 7, 8,
9 9,10,11,12,
10 13,14,15,16;
11 cout << "Block in the middle" << endl;
12 cout << m.block<2,2>(1,1) << endl << endl;
13
14 for (int i = 1; i <= 3; ++i)
15
16 {
17
18 cout << "Block of size " << i << "x" << i << endl;
19 cout << m.block(0,0,i,i) << endl << endl;
20 }
21 }
输出
Block in the middle
6 7
10 11
Block of size 1x1
1
Block of size 2x2
1 2
5 6
Block of size 3x3
1 2 3
5 6 7
9 10 11
作为左值
1 int main()
2
3 {
4
5 Array22f m;
6
7 m << 1,2,
8
9 3,4;
10
11 Array44f a = Array44f::Constant(0.6);
12
13 cout << "Here is the array a:" << endl << a << endl << endl;
14
15 a.block<2,2>(1,1) = m;
16
17 cout << "Here is now a with m copied into its central 2x2 block:" << endl << a << endl << endl;
18
19 a.block(0,0,2,3) = a.block(2,1,2,3);
20
21 cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:" << endl << a << endl << endl;
22
23 }
输出
Here is the array a:
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
Here is now a with m copied into its central 2x2 block:
0.6 0.6 0.6 0.6
0.6 1 2 0.6
0.6 3 4 0.6
0.6 0.6 0.6 0.6
Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:
3 4 0.6 0.6
0.6 0.6 0.6 0.6
0.6 3 4 0.6
0.6 0.6 0.6 0.6
3. 行和列
1 int main()
2
3 {
4
5 Eigen::MatrixXf m(3,3);
6
7 m << 1,2,3,
8
9 4,5,6,
10
11 7,8,9;
12
13 cout << "Here is the matrix m:" << endl << m << endl;
14
15 cout << "2nd Row: " << m.row(1) << endl;
16
17 m.col(2) += 3 * m.col(0);
18
19 cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
20
21 cout << m << endl;
22
23 }
输出
Here is the matrix m:
1 2 3
4 5 6
7 8 9
2nd Row: 4 5 6
After adding 3 times the first column into the third column, the matrix m is:
1 2 6
4 5 18
7 8 30
4. 角相关操作
1 int main()
2
3 {
4
5 Eigen::Matrix4f m;
6
7 m << 1, 2, 3, 4,
8
9 5, 6, 7, 8,
10
11 9, 10,11,12,
12
13 13,14,15,16;
14
15 cout << "m.leftCols(2) =" << endl << m.leftCols(2) << endl << endl;
16
17 cout << "m.bottomRows<2>() =" << endl << m.bottomRows<2>() << endl << endl;
18
19 m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose();
20
21 cout << "After assignment, m = " << endl << m << endl;
22
23 }
输出
m.leftCols(2) =
1 2
5 6
9 10
13 14
m.bottomRows<2>() =
9 10 11 12
13 14 15 16
After assignment, m =
8 12 16 4
5 6 7 8
9 10 11 12
13 14 15 16
5. vectors的块操作