Suppose you have a matrix A
:
1 2
3 4
There are two flattenings:
1
2
3
4
and
1
3
2
4
If the default (ColMajor
) storage type is used, we can get the latter as
VectorXd v = Map<const VectorXd>(A.data(), A.size())
This only copies the data once.
But to get the former, the best I can think of is
MatrixXd At = A.transpose()
VectorXd v = Map<const VectorXd>(At.data(), At.size())
This copies the data twice, unfortunately.
Somewhat confusingly (to me at least)
VectorXd v = Map<const VectorXd>(A.transpose().data(), A.size())
compiles, but produces completely identical results as not having the transpose
there.
See also: Eigen Convert Matrix to Vector