liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace Eigen {
 
/** \eigenManualPage TutorialReshapeSlicing Reshape and Slicing
 
%Eigen does not expose convenient methods to take slices or to reshape a matrix yet.
Nonetheless, such features can easily be emulated using the Map class.
 
\eigenAutoToc
 
\section TutorialReshape Reshape
 
A reshape operation consists in modifying the sizes of a matrix while keeping the same coefficients.
Instead of modifying the input matrix itself, which is not possible for compile-time sizes, the approach consist in creating a different \em view on the storage using class Map.
Here is a typical example creating a 1D linear view of a matrix:
 
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_ReshapeMat2Vec.cpp
</td>
<td>
\verbinclude Tutorial_ReshapeMat2Vec.out
</td></tr></table>
 
Remark how the storage order of the input matrix modifies the order of the coefficients in the linear view.
Here is another example reshaping a 2x6 matrix to a 6x2 one:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_ReshapeMat2Mat.cpp
</td>
<td>
\verbinclude Tutorial_ReshapeMat2Mat.out
</td></tr></table>
 
 
 
\section TutorialSlicing Slicing
 
Slicing consists in taking a set of rows, columns, or elements, uniformly spaced within a matrix.
Again, the class Map allows to easily mimic this feature.
 
For instance, one can skip every P elements in a vector:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_SlicingVec.cpp
</td>
<td>
\verbinclude Tutorial_SlicingVec.out
</td></tr></table>
 
One can olso take one column over three using an adequate outer-stride or inner-stride depending on the actual storage order:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_SlicingCol.cpp
</td>
<td>
\verbinclude Tutorial_SlicingCol.out
</td></tr></table>
 
*/
 
}