![]() |
Eigen
5.0.1
|
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.
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.
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.
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.
Resizing for vectors behaves the same way as for matrices. You provide the new size as an argument to resize().
Resizing for arrays behaves the same way as for matrices. You provide the new number of rows and columns as arguments to resize().
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).
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.
If you need to resize a matrix while keeping its current values, use conservativeResize(rows, cols) .
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:
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.
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.
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.