Reputation: 5789
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 x
is a VectorXf
?
Upvotes: 2
Views: 1506
Reputation: 29205
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);
Upvotes: 3