Eigen
3.3.90 (mercurial changeset 94875feeeeb9)

There are two different storage orders for matrices and twodimensional arrays: columnmajor and rowmajor. This page explains these storage orders and how to specify which one should be used.
The entries of a matrix form a twodimensional grid. However, when the matrix is stored in memory, the entries have to somehow be laid out linearly. There are two main ways to do this, by row and by column.
We say that a matrix is stored in rowmajor order if it is stored row by row. The entire first row is stored first, followed by the entire second row, and so on. Consider for example the matrix
If this matrix is stored in rowmajor order, then the entries are laid out in memory as follows:
On the other hand, a matrix is stored in columnmajor order if it is stored column by column, starting with the entire first column, followed by the entire second column, and so on. If the above matrix is stored in columnmajor order, it is laid out as follows:
This example is illustrated by the following Eigen code. It uses the PlainObjectBase::data() function, which returns a pointer to the memory location of the first entry of the matrix.
Example  Output 

Matrix<int, 3, 4, ColMajor> Acolmajor;
Acolmajor << 8, 2, 2, 9,
9, 1, 4, 4,
3, 5, 4, 5;
cout << "The matrix A:" << endl;
cout << Acolmajor << endl << endl;
cout << "In memory (columnmajor):" << endl;
for (int i = 0; i < Acolmajor.size(); i++)
cout << *(Acolmajor.data() + i) << " ";
cout << endl << endl;
Matrix<int, 3, 4, RowMajor> Arowmajor = Acolmajor;
cout << "In memory (rowmajor):" << endl;
for (int i = 0; i < Arowmajor.size(); i++)
cout << *(Arowmajor.data() + i) << " ";
cout << endl;
 The matrix A: 8 2 2 9 9 1 4 4 3 5 4 5 In memory (columnmajor): 8 9 3 2 1 5 2 4 4 9 4 5 In memory (rowmajor): 8 2 2 9 9 1 4 4 3 5 4 5 
The storage order of a matrix or a twodimensional array can be set by specifying the Options
template parameter for Matrix or Array. As The Matrix class explains, the Matrix class template has six template parameters, of which three are compulsory (Scalar
, RowsAtCompileTime
and ColsAtCompileTime
) and three are optional (Options
, MaxRowsAtCompileTime
and MaxColsAtCompileTime
). If the Options
parameter is set to RowMajor
, then the matrix or array is stored in rowmajor order; if it is set to ColMajor
, then it is stored in columnmajor order. This mechanism is used in the above Eigen program to specify the storage order.
If the storage order is not specified, then Eigen defaults to storing the entry in columnmajor. This is also the case if one of the convenience typedefs (Matrix3f
, ArrayXXd
, etc.) is used.
Matrices and arrays using one storage order can be assigned to matrices and arrays using the other storage order, as happens in the above program when Arowmajor
is initialized using Acolmajor
. Eigen will reorder the entries automatically. More generally, rowmajor and columnmajor matrices can be mixed in an expression as we want.
So, which storage order should you use in your program? There is no simple answer to this question; it depends on your application. Here are some points to keep in mind: