The most general block operation in Eigen is called .block() . There are two versions, whose syntax is as follows:
Block operation | Version constructing a dynamic-size block expression | Version constructing a fixed-size block expression |
---|---|---|
Block of size (p,q) , starting at (i,j) |
matrix.block(i,j,p,q);
|
matrix.block<p,q>(i,j);
|
As always in Eigen, indices start at 0.
Both versions can be used on fixed-size and dynamic-size matrices and arrays. These two expressions are semantically equivalent. The only difference is that the fixed-size version will typically give you faster code if the block size is small, but requires this size to be known at compile time.
The following program uses the dynamic-size and fixed-size versions to print the values of several blocks inside a matrix.
Example: | Output: |
---|---|
#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 |