Ela782
Ela782

Reputation: 5181

Mapping (reshaping) a static Eigen Matrix

I've got a 2 x 4 matrix that I want to represent as a 1 x 8 vector, put the result from colPivHouseholderQr().solve in it, and then return that 2 x 4 matrix. I've read this and tried the following:

Matrix<float, 2, 4> X;
Eigen::Map<Matrix<float, 1, 8>> X_vec(X.data(), 2, 4); // Run-time error
X_vec = A.colPivHouseholderQr().solve(b);
return X;

However I get an error at run time:

Assertion failed: v == T(Value), file eigen\eigen\src/Core/util/XprHelper.h, line 110

Is there a way to accomplish this?

Upvotes: 1

Views: 689

Answers (1)

m7913d
m7913d

Reputation: 11072

The second and third argument of the Eigen::Map constructor are respectively the number of rows and columns of the new matrix. For fixed sized matrices, they should be the same as those provided as template arguments. So, you should write:

Eigen::Map<Matrix<float, 1, 8>> X_vec(X.data(), 1, 8);

As an alternative, you can use the overloaded constructor which is provided for fixed size matrices only as documented by Eigen:

Eigen::Map<Matrix<float, 1, 8>> X_vec(X.data());

Constructor in the fixed-size case.

Note that I suggest you to use this latest version, because it is less error prone and the other overloads are only for dynamic size matrices if your read the documentation very strictly:

Constructor in the dynamic-size matrix case.

Upvotes: 1

Related Questions