04 块操作

块操作

[TOC]

block函数

fixed-size version

matrix.block<p, q>(i, j);		// 从(i ,j)开始的p行q列 组成的block

dynamic-size version

matrix.block(i, j, p, q);		// 从(i ,j)开始的p行q列 组成的block

案例

#include <Eigen/Dense>
#include <iostream>

using namespace std;

int main()
{
Eigen::MatrixXf m(4,4);
m << 1, 2, 3, 4,
5, 6, 7, 8,
9,10,11,12,
13,14,15,16;
cout << "Block in the middle" << endl;
cout << m.block<2,2>(1,1) << endl << endl;
for (int i = 1; i <= 3; ++i)
{
cout << "Block of size " << i << "x" << i << endl;
cout << m.block(0,0,i,i) << endl << endl;
}
}

/*

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
*/

lvalue & rvalue

.block()函数的返回值通常作为一个右值,但也可以是一个左值使用。

#include <Eigen/Dense>
#include <iostream>

int main()
{
Eigen::Array22f m;
m << 1,2,
3,4;
Eigen::Array44f a = Eigen::Array44f::Constant(0.6);
std::cout << "Here is the array a:\n" << a << "\n\n";
a.block<2,2>(1,1) = m;
std::cout << "Here is now a with m copied into its central 2x2 block:\n" << a << "\n\n";
a.block(0,0,2,3) = a.block(2,1,2,3);
std::cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:\n" << a << "\n\n";
}

/*

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 2x3 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
*/

col()和row()

​ 用于处理矩阵的行列操作。

#include <Eigen/Dense>
#include <iostream>

using namespace std;

int main()
{
Eigen::MatrixXf m(3,3);
m << 1,2,3,
4,5,6,
7,8,9;
cout << "Here is the matrix m:" << endl << m << endl;
cout << "2nd Row: " << m.row(1) << endl;
m.col(2) += 3 * m.col(0);
cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
cout << m << endl;
}
/*

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
*/

常见Corner相关的函数

Block operation dynamic-size block expression fixed-size block expression
左上角的 p * q 块* matrix.topLeftCorner(p,q); matrix.topLeftCorner<p,q>();
左下角的 p * q 块* matrix.bottomLeftCorner(p,q); matrix.bottomLeftCorner<p,q>();
右上 p * q 块* matrix.topRightCorner(p,q); matrix.topRightCorner<p,q>();
右下角的 p * q 块* matrix.bottomRightCorner(p,q); matrix.bottomRightCorner<p,q>();
包含前 q 行的块* matrix.topRows(q); matrix.topRows<q>();
包含最后 q 行的块* matrix.bottomRows(q); matrix.bottomRows<q>();
包含前 p 列的块* matrix.leftCols(p); matrix.leftCols<p>();
包含最后 q 列的块* matrix.rightCols(q); matrix.rightCols<q>();
包含从 i 开始的 q 列的块* matrix.middleCols(i,q); matrix.middleCols<q>(i);
包含从 i 开始的 q 行的块* matrix.middleRows(i,q); matrix.middleRows<q>(i);
#include <Eigen/Dense>
#include <iostream>

using namespace std;

int main()
{
Eigen::Matrix4f m;
m << 1, 2, 3, 4,
5, 6, 7, 8,
9, 10,11,12,
13,14,15,16;
cout << "m.leftCols(2) =" << endl << m.leftCols(2) << endl << endl;
cout << "m.bottomRows<2>() =" << endl << m.bottomRows<2>() << endl << endl;
m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose();
cout << "After assignment, m = " << endl << m << endl;
}

/*
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
*/

Vector的block运算

Block operation dynamic-size block expression fixed-size block expression
包含第一个元素的块n* vector.head(n); vector.head<n>();
包含最后一个元素的块n* vector.tail(n); vector.tail<n>();
包含元素的块,从位置开始i开始n* vector.segment(i,n); vector.segment<n>(i);
#include <Eigen/Dense>
#include <iostream>

using namespace std;

int main()
{
Eigen::ArrayXf v(6);
v << 1, 2, 3, 4, 5, 6;
cout << "v.head(3) =" << endl << v.head(3) << endl << endl;
cout << "v.tail<3>() = " << endl << v.tail<3>() << endl << endl;
v.segment(1,4) *= 2;
cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl;
}

/*
v.head(3) =
1
2
3

v.tail<3>() =
4
5
6

after 'v.segment(1,4) *= 2', v =
1
4
6
8
10
6
*/