2

In eigen c++, how do you map a vectorXf to a matrixXf (of appropriate dimensions)

(there is good docs on how to do it for external objects so i know we can do:

MatrixXf x_cen=Map<MatrixXf>(*x,*n,*p);

but what if xis a VectorXf?

user189035
  • 5,589
  • 13
  • 52
  • 112

1 Answers1

3

You can use the .data() member function followed by Map:

VectorXf vec(rows*cols);
vec = ...;
Map<MatrixXf> vec_view_as_a_matrix(vec.data(), rows, cols);

Then you can use vec_view_as_a_matrix just like any Eigen objects, modifications to vec_view_as_a_matrix will be reported to vec as well since they are sharing the memory. If you want to copy to a new MatrixXf object, then use the construction you wrote:

MatrixXf x_cen = Map<MatrixXf>(vec.data(), rows, cols);
ggael
  • 28,425
  • 2
  • 65
  • 71