Registered Member
|
when I use Eigen, I need to exchange data between different program, and you know Eigen is not a standard data structure, so std:vector should be a better media, and sometimes I need to manipulate a matrix like array.
I read the tutorial but I couldn't find data exchange function, can any body tell me some method? |
Registered Member
|
Look at Map: http://eigen.tuxfamily.org/dox/TutorialMapClass.html
std::vector stores it's data internally in an array ( vec.data() ) that you can map the matrix to. Just remember that, by default, the matrix will be mapped in column-major order. |
Registered Member
|
Thank you for your reply.
I find the method to read data from a C array or C++ std::vector to Eigen matrix, but I can't find the way to export the data from Eigen matrix to std::vector, can you give me some more suggestion? |
Registered Member
|
I hope not to be wrong here, but
the short anwer is, that the memory location of the data is shared. Both the eigen vector and the std:vector are using the same data. Therefore changing the eigen vector is in effect changing the mapped std::vector (Sorry to not post any code, but I written not a single line in eigen yet. |
Moderator
|
silent_missile: If you already have an Eigen::Matrix object, then you can access its internal data with the .data() function. However, std::vector<> is not able to map external memory meaning you will have to perform a copy, e.g.:
MatrixXf A; std::vector<float> v; Map<MatrixXf>(v.data(), m, n) = A; An alternative is to allocate memory through std::vector: std::vector<float> v(m*n); and use a named Eigen's object: Map<MatrixXf> A(v.data(), m, n); then you can use A just like a MatrixXf. All changes will be reported into 'v' since v and A share the same data memory. |
Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell