eigen::vectorXf to MatriXf map

纵然是瞬间 提交于 2019-12-11 00:57:24

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/13395431/eigenvectorxf-to-matrixf-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!