I can't figure out if and how it's possible to map a two-dimensional double array to an Eigen::Matrix.
Is it possible to map an array double d[][]
which I receive as double** p
to an Eigen::Matrix?
While one-dimensinal arrays work fine, I wasn't able to map p
to Eigen::Map<Eigen::Matrix<double, n, n>>
. Is that possible and how could it be done? The size n
is not really constant, but I could accept a hard coded size.
I tried several versions, but none worked. I thought the following should work (assume the size n
would be 4).
Eigen::Map<Eigen::Matrix<double, 4, 4>> p_OUTPUT(&p[0][0]);
The code compiles and runs, but only the elements of the first column and the first element of the second column map the correct values. Using p[0]
as argument yields the same result.
Other versions I tried (for example without the &
) did not compile.
The answer you gave yourself is kind of a clue: Eigen, by default, stores matrices in column-major format, meaning that elements from this matrix:
Are stored in a big linear array as:
Your data (judging from your answer) is in row-major format, which is why you're pulling out memory-contiguous rows from your array and assigning them into rows in your result. You can tell
Map
that your data is inRowMajor
format, and it should read your data correctly:For the sake of completeness, I found a solution. As mentioned here or here the storage in not contiguous memory is the problem.
The following solution worked for me.