将Eigen :: MatrixXf转换为2D std :: vector [英] Converting Eigen::MatrixXf to 2D std::vector
问题描述
有没有比点对点复制值更优雅的解决方案了?
像这样的东西适用于一维矢量...
Is there a more elegant solution than to copy the values point to point?!
Something like this works for a 1D vector...
vector<float> vec(mat.data(), mat.data() + mat.rows() * mat.cols());
我尝试了GCC编译器针对vector<vector>,但没有解决...
I tried various other alternatives that were suggested by the GCC compiler for vector< vector > but nothing worked out...
推荐答案
Eigen :: MatrixXf
使用有效的线性内存,而 vector
为 vector
表示非常不同的数据类型.
Eigen::MatrixXf
uses efficient linear memory, while a vector
of vector
would represent a very different datatype.
因此,对于多维矢量,您必须逐块读取矩阵并将这些值复制到最远的矢量中.
For multidimentional vector, you would therefore have to read the matrix block by block and copy those values to the outmost vectors.
另一种方法是将这些值复制到具有特定访问器的基于 vector
的类中,但是最终将重建一个类似Matrix的类.
Another way would be to copy the values to a vector
based class with specific accessors ... but that would end up reconstructing a Matrix like class.
您为什么要这样做?您尝试提供哪种访问方式?也许您应该尝试使用 eigen :: matrix
接口
Why do you want to do that ? What kind of access are you trying to provide ? Maybe you should rather try to do similar access using the eigen::matrix
interface
Eigen::MatrixXf m(2,3);
std::vector<std::vector<T>> v;
for (int i=0; i<m.rows(); ++i)
{
const float* begin = &m.row(i).data()[0];
v.push_back(std::vector<float>(begin, begin+m.cols()));
}
这篇关于将Eigen :: MatrixXf转换为2D std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!