I have two eigen matrices and I would like to concatenate them, like in matlab cat(0, A, B)
Is there anything equivalent in eigen?
Thanks.
You can use the comma initializer syntax for that.
Horizontally:
MatrixXd C(A.rows(), A.cols()+B.cols()); C << A, B;
Vertically:
// eigen uses provided dimensions in declaration to determine // concatenation direction MatrixXd D(A.rows()+B.rows(), A.cols()); // <-- D(A.rows() + B.rows(), ...) D << A, B; // <-- syntax is the same for vertical and horizontal concatenation
For readability, one might format vertical concatenations with whitespace:
D << A, B; // <-- But this is for readability only.
I'd use Eigen's block indexing in a way similar to this post (which concatenates to an existing matrix).
The block indexing avoids the direction ambiguity in the accepted approach, and is pretty compact syntax. The following is equivalent to C = cat(2, A, B)
in MATLAB:
MatrixXd C(A.rows(), A.cols()+B.cols()); C.leftCols(A.cols()) = A; C.rightCols(B.cols()) = B;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With