## How to copy Eigen Matrix to a POD double array?

pktrain
Registered Member
Posts
2
Karma
0
OS

### How to copy Eigen Matrix to a POD double array?

Fri Jun 10, 2011 10:32 am
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...
ggael
Moderator
Posts
3403
Karma
19
OS

### Re: How to copy Eigen Matrix to a POD double array?

Fri Jun 10, 2011 10:46 am
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

### Re: How to copy Eigen Matrix to a POD double array?

Fri Jun 10, 2011 10:57 am
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

### Re: How to copy Eigen Matrix to a POD double array?

Fri Mar 10, 2017 10:06 am
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 pointerMap<MatrixXd>( targetArray, m.rows(), m.cols() ) = m;`

Am I doing anything wrong here?

ggael
Moderator
Posts
3403
Karma
19
OS

### Re: How to copy Eigen Matrix to a POD double array?

Fri Mar 10, 2017 10:40 am
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();

## Who is online

Registered users: adamsmark, Baidu [Spider], Bing [Bot], Exabot [Bot], flherne, geoffreywheeler, Google [Bot], KDENVIDIA, raddison, razvanr, scummos, Sogou [Bot], Yahoo [Bot]