For flexibility, I'm loading data into dynamic-sized matrices (e.g. Eigen::MatrixXf
) using the C++ library Eigen. I've written some functions which require mixed- or fixed-sized matrices as parameters (e.g. Eigen::Matrix<float, 3, Eigen::Dynamic>
or Eigen::Matrix4f
). Assuming I do the proper assertions for row and column size, how can I convert the dynamic matrix (size set at runtime) to a fixed matrix (size set at compile time)?
The only solution I can think of is to map it, for example:
Eigen::MatrixXf dyn = Eigen::MatrixXf::Random(3, 100);
Eigen::Matrix<float, 3, Eigen::Dynamic> fixed =
Eigen::Map<float, 3, Eigen::Dynamic>(dyn.data(), 3, dyn.cols());
But it's unclear to me if that will work either because the fixed size map constructor doesn't accept rows and columns as parameters in the docs. Is there a better solution? Simply assigning dynamic- to fixed-sized matrices doesn't work.
Eigen::Map
. – m7913dEigen::Matrix<float, 3, Eigen::Dynamic> fixed = dyn;
should work without problems. If not, please add the error you get when writing this. This conversoin does result in copying all coefficients, however (which may not be what you want). – chtz