This forum has been archived. All content is frozen. Please use KDE Discuss instead.

can I exchange data between matrix, array and std:vector

Tags: None
(comma "," separated)
silent_missile
Registered Member
Posts
7
Karma
0
OS
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?
Manhigh
Registered Member
Posts
10
Karma
0
OS
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.
silent_missile
Registered Member
Posts
7
Karma
0
OS
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?
FMD
Registered Member
Posts
25
Karma
0
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. :-)
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
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.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell