https://eigen.tuxfamily.org/dox-devel/group__TutorialReshape.html
注意,需要Eigen3.4版本
Since the version 3.4, Eigen exposes convenient methods to reshape a matrix to another matrix of different sizes or vector. All cases are handled via the DenseBase::reshaped(NRowsType,NColsType) and DenseBase::reshaped() functions. Those functions do not perform in-place reshaping, but instead return a view on the input expression.
The more general reshaping transformation is handled via: reshaped(nrows,ncols). Here is an example reshaping a 4x4 matrix to a 2x8 one:
code:注意,默认是列优先,matlab的reshape是列优先
Matrix4i m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(2, 8):" << endl << m.reshaped(2, 8) << endl;
output:
Here is the matrix m:
-10 1 4 7
-8 -6 9 -10
5 -10 -2 -9
-1 4 0 1
Here is m.reshaped(2, 8):
-10 5 1 -10 4 -2 7 -9
-8 -1 -6 4 9 0 -10 1
code:注意,默认是列优先,matlab的reshape是列优先
Eigen::VectorXd M1(9); // Column-major storage
M1 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(3,3):" << endl << m.reshaped(3,3) << endl;
output:
Here is the matrix m:
1
2
3
4
5
6
7
8
9
Here is m.reshaped(2, 8):
1 4 7
2 5 8
3 6 9
By default, the input coefficients are always interpreted in column-major order regardless of the storage order of the input expression. For more control on ordering, compile-time sizes, and automatic size deduction, please see de documentation of DenseBase::reshaped(NRowsType,NColsType) that contains all the details with many examples.
A very common usage of reshaping is to create a 1D linear view over a given 2D matrix or expression. In this case, sizes can be deduced and thus omitted as in the following example:
Matrix4i m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped().transpose():" << endl << m.reshaped().transpose() << endl;
cout << "Here is m.reshaped().transpose(): " << endl << m.reshaped().transpose() << endl;
output:
Here is the matrix m:
-10 1 4 7
-8 -6 9 -10
5 -10 -2 -9
-1 4 0 1
Here is m.reshaped().transpose():
-10 -8 5 -1 1 -6 -10 4 4 9 -2 0 7 -10 -9 1
Here is m.reshaped().transpose():
-10 1 4 7 -8 -6 9 -10 5 -10 -2 -9 -1 4 0 1
This shortcut always returns a column vector and by default input coefficients are always interpreted in column-major order. Again, see the documentation of DenseBase::reshaped() for more control on the ordering.
The above examples create reshaped views, but what about reshaping inplace a given matrix? Of course this task in only conceivable for matrix and arrays having runtime dimensions. In many cases, this can be accomplished via PlainObjectBase::resize(Index,Index):
MatrixXi m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(2, 8):" << endl << m.reshaped(2, 8) << endl;
m.resize(2,8);
cout << "Here is the matrix m after m.resize(2,8):" << endl << m << endl
output:
Here is the matrix m:
-10 1 4 7
-8 -6 9 -10
5 -10 -2 -9
-1 4 0 1
Here is m.reshaped(2, 8):
-10 5 1 -10 4 -2 7 -9
-8 -1 -6 4 9 0 -10 1
Here is the matrix m after m.resize(2,8):
-10 5 1 -10 4 -2 7 -9
-8 -1 -6 4 9 0 -10 1
However beware that unlike reshaped, the result of resize depends on the input storage order. It thus behaves similarly to reshaped:
Matrix m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(2, 8):" << endl << m.reshaped(2, 8) << endl;
cout << "Here is m.reshaped(2, 8):" << endl << m.reshaped(2, 8) << endl;
m.resize(2,8);
cout << "Here is the matrix m after m.resize(2,8):" << endl << m << endl;
output
Here is the matrix m:
-10 -8 5 -1
1 -6 -10 4
4 9 -2 0
7 -10 -9 1
Here is m.reshaped(2, 8):
-10 4 -8 9 5 -2 -1 0
1 7 -6 -10 -10 -9 4 1
Here is m.reshaped(2, 8):
-10 -8 5 -1 1 -6 -10 4
4 9 -2 0 7 -10 -9 1
Here is the matrix m after m.resize(2,8):
-10 -8 5 -1 1 -6 -10 4
4 9 -2 0 7 -10 -9 1
Finally, assigning a reshaped matrix to itself is currently not supported and will result to undefined-behavior because of aliasing . The following is forbidden:
A = A.reshaped(2,8);
This is OK:
A = A.reshaped(2,8).eval();