Eigen  5.0.1
 
Loading...
Searching...
No Matches

Resizing with resize()

The most basic method to change the size of matrices or vectors is resize(rows, cols) . It takes the new number of rows and columns as arguments.

MatrixXd m(2,2);
m << 1, 2, 3, 4;
m.resize(3,3);
// m is now 3x3.
// OLD values are lost. NEW values are uninitialized.
Matrix< double, Dynamic, Dynamic > MatrixXd
Dynamic×Dynamic matrix of type double.
Definition Matrix.h:489

The resize() method is destructive if the total number of coefficients (rows x columns) differs from the previous one. Meaning that all previous values are lost and the newly allocated coefficients are uninitialized. You should fill them before use.

The special case of "No-Op" resizing

If you resize a matrix while keeping the total number of coefficients unchanged, the existing values are preserved in memory. (Meaning when old_rows x old_cols = new_rows x new_cols)

However, because Eigen stores matrices in column-major order by default, the logical position of these values may change.

MatrixXd m(2,2);
m << 1, 2, 3, 4;
// m is now: 1 2
// 3 4
// Memory storage: [1, 3, 2, 4]
// Resizing to 1x4 (total size 4 is unchanged)
m.resize(1,4);
// m is now: 1 3 2 4
// The memory [1, 3, 2, 4] was not touched, but is now interpreted as a 1x4 matrix.

Resizing only one dimension

To resize only one dimension while leaving the other unchanged, you can pass Eigen::NoChange as the parameter for the dimension you wish to keep.

MatrixXd m(2,2);
// Resize rows to 5, keep columns at 2
m.resize(5, Eigen::NoChange);
// Resize columns to 3, keep rows at 5
m.resize(Eigen::NoChange, 3);

Resizing vectors

Resizing for vectors behaves the same way as for matrices. You provide the new size as an argument to resize().

VectorXd v(3);
v << 1, 2, 3;
v.resize(5);
// v is now of size 5 and the values are uninitialized.
Matrix< double, Dynamic, 1 > VectorXd
Dynamic×1 vector of type double.
Definition Matrix.h:489

Resizing arrays

Resizing for arrays behaves the same way as for matrices. You provide the new number of rows and columns as arguments to resize().

ArrayXXf a(2,2);
a << 1, 2, 3, 4;
a.resize(3,3);
// a is now 3x3 and the values are uninitialized.

Resizing to match another object with resizeLike()

You can resize a matrix or vector to match the dimensions of another object using resizeLike(eigenBase) . This method is also destructive (data is lost).

MatrixXd m(2,2);
MatrixXd n(4,4);
m.resizeLike(n);
// m is now 4x4.

Note on Vectors: When applied to vectors, resizeLike() matches the size (number of coefficients) of the other object, but maintains the row/column orientation of the vector being resized.

VectorXd c(5);
// r is resized to be a row-vector of size 5 (1x5), matching c's size.
// It does NOT become a column-vector.
r.resizeLike(c);
Matrix< double, 1, Dynamic > RowVectorXd
1×Dynamic vector of type double.
Definition Matrix.h:489

Resizing with conservativeResize()

If you need to resize a matrix while keeping its current values, use conservativeResize(rows, cols) .

MatrixXd m(2,2);
m << 1, 2, 3, 4;
m.conservativeResize(3,3);
// m is now:
// 1 2 ?
// 3 4 ?
// ? ? ?
// The '?' are uninitialized values.

When using conservativeResize():

Just like resize(), you can resize vectors and arrays and keep previous values, using conservativeResize().

And just like resize(), you can use Eigen::NoChange to resize only one dimension conservatively:

MatrixXd m(2,2);
m << 1, 2, 3, 4;
// Add a new row (now 3x2), keeping existing values
m.conservativeResize(3, Eigen::NoChange);

Automatic resizing on assignment

By default, when you assign one matrix to another, Eigen automatically resizes the left-hand side to match the size of the right-hand side.

MatrixXf a(2,2);
MatrixXf b(4,4);
a = b; // a is now 4x4
Matrix< float, Dynamic, Dynamic > MatrixXf
Dynamic×Dynamic matrix of type float.
Definition Matrix.h:488

Disabling automatic resizing

In some applications, you may want to prevent automatic resizing to avoid unexpected memory allocations. You can disable this behavior by defining the EIGEN_NO_AUTOMATIC_RESIZING preprocessor macro.

If this macro is defined, the assignment a = b will trigger an assertion failure at runtime if the dimensions of a and b do not match.

#define EIGEN_NO_AUTOMATIC_RESIZING
#include <Eigen/Dense>
void function() {
MatrixXf a(2,2);
MatrixXf b(4,4);
a = b; // ERROR: Runtime assertion failure
}

Fixed-size matrices

Resizing methods are technically available on fixed-size matrices for API uniformity, but they will trigger an assertion failure if you try to actually change the dimensions. Because the dimensions of a fixed-size matrix (like Matrix4f) are determined at compile-time, they cannot be changed at runtime.

m.resize(4,4); // Legal, no-op
m.resize(5,5); // ERROR: Runtime assertion failure
constexpr void resize(Index rows, Index cols)
Definition PlainObjectBase.h:282
Matrix< float, 4, 4 > Matrix4f
4×4 matrix of type float.
Definition Matrix.h:488