15

I have two vectorXd in my program and I like to concatenate them into one vector, so that the second one's values goes after the first one, I found this for matrix but it doesn't seem to work on Vectors:

Eigen how to concatenate matrix along a specific dimension?

Community
  • 1
  • 1
user3178756
  • 555
  • 1
  • 5
  • 17

1 Answers1

17

Like so, assuming you have vec1 and vec2 already:

VectorXd vec_joined(vec1.size() + vec2.size());
vec_joined << vec1, vec2;

(Note that the vector types are simply typedefs of matrix types constrained to have only one column.)

Further reading: Advanced initialization

Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 12
    Shouldn't the first line above be `VectorXd vec_joined( vec1.rows() + vec2.rows() );`? Without first initializing the vector `vec_joined` it will spit assertion failures ... – nils Aug 25 '15 at 13:22
  • 1
    Would this syntax work with more than two vectors? For example, provided proper initialisation has been done before hand: `vec_joined << vec1, vec2, vec3;` – LNiederha Nov 30 '20 at 10:22