2

In MATLAB, I can do the following

A = [1 2 3; 4 5 6];
A(:)

to get:

ans =
1
4
2
5
3
6

How would I do this with an Eigen3 Matrix?

Peter
  • 415
  • 7
  • 13

1 Answers1

2

The best way is to use Map:

Map<VectorXd> v(A.data(),A.size());

because in this case Eigen knows at compile time that you now have a 1D vector.

Of course, the result will depend on the storage order of A, that is, for a column major matrix (the default):

[1 4 2 5 3 6]^T

and for a row-major one:

[1 2 3 4 5 6]^T
ggael
  • 28,425
  • 2
  • 65
  • 71
  • 1
    The second suggestion is a bad idea because it assumes that A is stored internally in row-major format with no offset between rows. In the simple example above, this will always be true, but in other cases it will not be. – Alex Flint Sep 14 '14 at 13:51
  • That's 'resize()' you want to use, 'conservativeResize()' won't work. See proper answer here: http://stackoverflow.com/questions/22881768/eigen-convert-matrix-to-vector – YvesgereY May 30 '16 at 09:55
  • hm, I've too fast of that one, you're right conservativeResize won't work here. – ggael May 30 '16 at 20:07