简体   繁体   English

从特征矩阵制作特征向量的标准向量

[英]Make std vector of Eigen vectors from Eigen Matrix

I have an a Eigen::MatrixXd that I made from std::vector of Eigen::Vector3d .我有一个由Eigen::MatrixXdstd::vector制作的Eigen::Vector3d It was easy.很容易。

I made some transform manipulation on that matrix and I want return result as std vector of Eigen::Vector3d .我对该矩阵进行了一些变换操作,我希望将结果作为Eigen::Vector3d的标准向量返回。

How could I make我怎么能做

std::vector<Eigen::Vector3d> form Eigen::Matrix3d? std::vector<Eigen::Vector3d>形成Eigen::Matrix3d?

Better stick with the std::vector object.最好坚持使用std::vector object。 Here is how I usually deal with such cases:以下是我通常处理此类情况的方式:

std::vector<Vector3d> vecs(n);
auto mat = Matrix3Xd::Map(vecs[0].data(), 3, vecs.size());

This creates a view on the data owned by vecs .这将创建vecs拥有的数据的视图。 Then play with mat as you like (except resizing of course,).然后随心所欲地玩mat (当然调整大小除外)。 eg::例如::

mat = my_affine * mat;

No need to copy back the values from mat to vecs , but of course, if you have a Matrix3Xf or MatrixXf already at hand and want to copy it to vecs , then simply write:无需将值从mat复制回vecs ,但当然,如果您手头已有Matrix3XfMatrixXf并想将其复制到vecs ,则只需编写:

mat = other_mat;

Provided that vecs.size() == other_mat.cols() , otherwise you need to resize vecs first and re-create a Map with new size.前提是vecs.size() == other_mat.cols() ,否则您需要先调整vecs的大小并重新创建具有新大小的Map

I agree with @ggael and @RHertel that you should stick with one representation.我同意@ggael 和@RHertel 的观点,即你应该坚持一种表示。 If you need to dynamically insert Vector3d objects, then std::vector is probably the better solution (and you can still access it like an Eigen object, using Eigen::Map as shown by ggael).如果您需要动态插入Vector3d对象,那么std::vector可能是更好的解决方案(并且您仍然可以像 Eigen object 一样使用Eigen::Map访问它,如 ggael 所示)。

Likewise, if you have a Matrix3Xd and want to use it column-wise in a standard algorithm, you can do that using .colwise().begin() and .colwise().end() which by itself won't copy any data (this requires the development branch of Eigen -- or the upcoming 3.4 version).同样,如果您有一个Matrix3Xd并希望在标准算法中按列使用它,您可以使用.colwise().begin().colwise().end()来做到这一点,它们本身不会复制任何数据(这需要 Eigen 的开发分支——或即将推出的 3.4 版本)。

This also gives an easy way to create an std::vector from an Eigen::Matrix3Xd :这也提供了一种从Eigen::Matrix3Xd创建std::vector的简单方法:

// `mat` needs to have 3 rows (at runtime)
std::vector<Eigen::Vector3d> vec(mat.colwise().begin(), mat.colwise().end());

Godbolt-Demo: https://godbolt.org/z/uCqZni Godbolt 演示: https://godbolt.org/z/uCqZni

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM