Mahdi Sheida
Mahdi Sheida

Reputation: 11

How to concatenate matrix?

I have 3 vertical eigen matrices and I would like to concatenate them, like in matlab C=[B1; B2; B3];. I want concatenate them, in this case: C(B1.cols()+B2.cols()+B3.cols(),1)

MatrixXd B1,B2,B3;
C<<B1,B2,B3;

Is there anything equivalent in eigen?

Upvotes: 1

Views: 4931

Answers (2)

Pidhorskyi
Pidhorskyi

Reputation: 1582

It is not very clear from your question, but I've understood it as you need to concatenate 3 vertical matrices of size (n, 1) to produce a matrix of size (m, 1), where m is the sum of all n.

If so, than you can try the following:

MatrixXd C(B1.rows() + B2.rows() + B3.rows(), 1);
C << B1, B2, B3;

For example, this code:

Eigen::MatrixXd B1(3, 1);
B1 << 1, 2, 3;

Eigen::MatrixXd B2(3, 1);
B2 << 42, 43, 44;

Eigen::MatrixXd B3(3, 1);
B3 << -1, -2, -3;

MatrixXd C(B1.rows() + B2.rows() + B3.rows(), 1);
C << B1, B2, B3;
std::cout << "Rows: " << C.rows() << std::endl;
std::cout << "Columns: " << C.cols() << std::endl << std::endl;
std::cout << C << std::endl;

would produce the following output:

Rows: 9
Columns: 1

 1
 2
 3
42
43
44
-1
-2
-3

Upvotes: 1

Ilio Catallo
Ilio Catallo

Reputation: 3180

Libigl provides a igl::cat function. Maybe it will do the trick.

Upvotes: 0

Related Questions