直接贴代码吧,使用的MatrixXd
和<<
运算符:
cpp
int main(int argc, char *argv[])
{
Eigen::MatrixXd B(2, 2);
B << 1, 2,
3, 4;
Eigen::MatrixXd C(2, 2);
C << 5, 6,
7, 8;
Eigen::MatrixXd D(2, 2);
D << 9, 10,
11, 12;
Eigen::MatrixXd H(2, 2);
H << 13, 14,
15, 16;
// Horizontal concatenation of B and C
Eigen::MatrixXd upper(B.rows(), B.cols() + C.cols());
upper << B, C;
std::cout << "Combined Matrix upper:\n" << upper << std::endl;
// Horizontal concatenation of D and H
Eigen::MatrixXd lower(D.rows()+ H.rows(), D.cols());
lower << D,
H;
std::cout << "Combined Matrix lower:\n" << lower << std::endl;
// Vertical concatenation of upper and lower
Eigen::MatrixXd A(B.rows() + D.rows(), B.cols() + C.cols());
A << B, C,
D, H;
std::cout << "Combined Matrix A:\n" << A << std::endl;
return 0;
}
效果如图