Reply to topic

How to copy Eigen Matrix to a POD double array?

pktrain
Registered Member
Posts
2
Karma
0
OS
Hello,

Per the first example found at http://eigen.tuxfamily.org/dox/classEigen_1_1Map.html, I have no problem using Map to map a double array to an Eigen Matrix. But I can find nowhere an example of mapping that Eigen Matrix back to the plain old double array.

Can someone please provide an example of the best way to do this without using loops?

Many thanks...
User avatar ggael
Moderator
Posts
3373
Karma
19
OS
the same:

MatrixXf m(rows,cols);
...
Map<MatrixXf>(ptr,rows,cols) = m;

Note that you can also name the mapped object and directly use it in expressions instead of copying it:

MatrixXf A, B;
Map<MatrixXf> m(ptr,rows,cols);
...
B += 0.5 * m * B;
b.diagonal() += 1;
...
pktrain
Registered Member
Posts
2
Karma
0
OS
ggael wrote:the same:

MatrixXf m(rows,cols);
...
Map<MatrixXf>(ptr,rows,cols) = m;

Note that you can also name the mapped object and directly use it in expressions instead of copying it:

MatrixXf A, B;
Map<MatrixXf> m(ptr,rows,cols);
...
B += 0.5 * m * B;
b.diagonal() += 1;
...


Works perfectly...exactly what I was looking for! :)
whizzwr
Registered Member
Posts
1
Karma
0
hello sorry for resurrecting old thread.

I'm trying tto do the exact thing described in the OP: copy Eigen Matrix to a POD double array.

However I always get segmentation fault while doing this,
apparently I'm not alone, as described here http://stackoverflow.com/questions/8443 ... to-c-array

Here is part of my code:
Code: Select all

  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2;
  m(0,1) = 1;
  m(1,1) = 4;

double *targetArray;                // NULL pointer

Map<MatrixXd>( targetArray, m.rows(), m.cols() ) = m;



Am I doing anything wrong here?

Thanks in advance.
User avatar ggael
Moderator
Posts
3373
Karma
19
OS
You need to allocate targetArray! Map<MatrixXd> is to map existing buffer.

If you don't want to copy the data but just need a plain pointer, then use double* ptr = mat.data();

 
Reply to topic

Bookmarks



Who is online

Registered users: aus9, Baidu [Spider], Bing [Bot], domson, flyfan, FreeSlave, Google [Bot], oshunluvr, paulus3005, scummos, Sogou [Bot], TheraHedwig, vishalrao, wolfi323, Yahoo [Bot]