user3294195 user3294195 - 23 days ago 6
C++ Question

Eigen::Ref for concatenating matrices

If I want to concatenate two matrices

A
and
B
, I would do

using Eigen::MatrixXd;
const MatrixXd A(n, p);
const MatrixXd B(n, q);
MatrixXd X(n, p+q);
X << A, B;


Now if
n
,
p
,
q
are large, defining
X
in this way would mean creating copies of
A
and
B
. Is it possible to define
X
as an
Eigen::Ref<MatrixXd>
instead?

Thanks.

Answer

No, Ref is not designed for that. We/You would need to define a new expression for that, that could be called Cat. If you only need to concatenate two matrices horizontally, in Eigen 3.3, this can be implemented in less than a dozen of lines of code as a nullary expression, see some exemple there.