And similarly, we define a self-explanatory typedef \c VectorXi as follows:
\code
typedef Matrix<int, Dynamic, 1> VectorXi;
\endcode
You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in:
\code
Matrix<float, 3, Dynamic>
\endcode
\section TutorialMatrixConstructors Constructors
A default constructor is always available, never performs any dynamic memory allocation, and never initializes the matrix coefficients. You can do:
\code
Matrix3f a;
MatrixXf b;
\endcode
Here,
\li \c a is a 3-by-3 matrix, with a plain float[9] array of uninitialized coefficients,
\li \c b is a dynamic-size matrix whose size is currently 0-by-0, and whose array of
coefficients hasn't yet been allocated at all.
Constructors taking sizes are also available. For matrices, the number of rows is always passed first.
For vectors, just pass the vector size. They allocate the array of coefficients
with the given size, but don't initialize the coefficients themselves:
\code
MatrixXf a(10,15);
VectorXf b(30);
\endcode
Here,
\li \c a is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients.
\li \c b is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients.
In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these
constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal:
\code
Matrix3f a(3,3);
\endcode
and is a no-operation.
Matrices and vectors can also be initialized from lists of coefficients.
Prior to C++11, this feature is limited to small fixed-size column or vectors up to size 4:
\code
Vector2d a(5.0, 6.0);
Vector3d b(5.0, 6.0, 7.0);
Vector4d c(5.0, 6.0, 7.0, 8.0);
\endcode
If C++11 is enabled, fixed-size column or row vectors of arbitrary size can be initialized by passing an arbitrary number of coefficients:
\code
Vector2i a(1, 2); // A column vector containing the elements {1, 2}
Matrix<int, 5, 1> b {1, 2, 3, 4, 5}; // A row-vector containing the elements {1, 2, 3, 4, 5}
Matrix<int, 1, 5> c = {1, 2, 3, 4, 5}; // A column vector containing the elements {1, 2, 3, 4, 5}
\endcode
In the general case of matrices and vectors with either fixed or runtime sizes,
coefficients have to be grouped by rows and passed as an initializer list of initializer list (\link Matrix::Matrix(const std::initializer_list<std::initializer_list<Scalar>>&) details \endlink):
\code
MatrixXi a { // construct a 2x2 matrix
{1, 2}, // first row
{3, 4} // second row
};
Matrix<double, 2, 3> b {
{2, 3, 4},
{5, 6, 7},
};
\endcode
For column or row vectors, implicit transposition is allowed.
This means that a column vector can be initialized from a single row:
\code
VectorXd a {{1.5, 2.5, 3.5}}; // A column-vector with 3 coefficients
RowVectorXd b {{1.0, 2.0, 3.0, 4.0}}; // A row-vector with 4 coefficients
%Matrix and vector coefficients can be conveniently set using the so-called \em comma-initializer syntax.
For now, it is enough to know this example:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr>
<td>\include Tutorial_commainit_01.cpp </td>
<td>\verbinclude Tutorial_commainit_01.out </td>
</tr></table>
The right-hand side can also contain matrix expressions as discussed in \ref TutorialAdvancedInitialization "this page".
\section TutorialMatrixSizesResizing Resizing
The current size of a matrix can be retrieved by \link EigenBase::rows() rows()\endlink, \link EigenBase::cols() cols() \endlink and \link EigenBase::size() size()\endlink. These methods return the number of rows, the number of columns and the number of coefficients, respectively. Resizing a dynamic-size matrix is done by the \link PlainObjectBase::resize(Index,Index) resize() \endlink method.
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr>
<td>\include tut_matrix_resize.cpp </td>
<td>\verbinclude tut_matrix_resize.out </td>
</tr></table>
The resize() method is a no-operation if the actual matrix size doesn't change; otherwise it is destructive: the values of the coefficients may change.
If you want a conservative variant of resize() which does not change the coefficients, use \link PlainObjectBase::conservativeResize() conservativeResize()\endlink, see \ref TopicResizing "this page" for more details.
All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually
resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure;
\section TutorialMatrixAssignment Assignment and resizing
Assignment is the action of copying a matrix into another, using \c operator=. Eigen resizes the matrix on the left-hand side automatically so that it matches the size of the matrix on the right-hand size. For example: