Converting Eigen::MatrixXf to 2D std::vector

怎甘沉沦 提交于 2019-12-25 15:57:52

问题


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

I tried various other alternatives that were suggested by the GCC compiler for vector< vector > but nothing worked out...


回答1:


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.

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.

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

Conversion

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


来源:https://stackoverflow.com/questions/29242460/converting-eigenmatrixxf-to-2d-stdvector

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