lorniper
lorniper

Reputation: 638

Mapping row-major array to column-majored Eigen Matrix

I want to map from a C-type array to a Column majored Eigen matrix.

The mapping itself is using the RowMajor type,

so I tried

std::vector<double> a(9);
double *p= a.data();
Eigen::MatrixXd M=Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>>(p)

I got what I expected(the order of M.data()), however, if the dimension(3) in the template is not known at compile time, this method doesn't work... any solution?

Upvotes: 6

Views: 6200

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

I assume that you wrote:

Eigen::MatrixXd M=Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(p);

This doesn't let the map know what the dimensions should be. You have to add that in the constructor:

std::vector<double> a{1,2,3,4,5,6,7,8,9};
double *p = a.data();
std::cout << Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>>(p) << "\n\n";
std::cout << Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(p, 3, 3) << "\n\n";

std::cout << Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::ColMajor>>(p) << "\n\n";
std::cout << Eigen::Map<Eigen::MatrixXd>(p, 3, 3) << "\n\n";

Upvotes: 7

Related Questions