本征如何在容器循环中干净地附加矩阵?

Eigen how do I cleanly append matrices in a container loop?

我有这个使用 Eigen 的代码:

struct Boundary
{
    Eigen::VectorXi VL;
    Eigen::MatrixXi EL;
    double length;
    bool isHole;
    bool isBoundary;
    bool isMainBoundary;
};

    int boundaryLoops = {};
    int boundariesNo = {};
    int holes = {};
    std::vector<Boundary> boundaries = {};

    MatrixXi SEUV;

    // ***** get all those boundaries here ****
    if (!CheckBoundaries(boundaryLoops, boundariesNo, holes, 
    boundaries))return false;

    // resize all loops container
    long size = {};
    for (auto &&boundary : boundaries){
        size += boundary.EL.rows();
    }
    SEUV.resize(size, 2);

    for (auto &&boundary : boundaries)
    {

        SEUV << boundary.EL; // Too few coefficients passed to comma initializer (operator<<)
        SEUV << SEUV, boundary.EL; // Too many rows passed to comma initializer (operator<<)
    }

除了通常的列数和行数计数之外,还有 swift 方法可以做到这一点吗?这是反复出现的,所以我正在寻找最干净的解决方案

谢谢

如果您想按行追加矩阵,则必须确保列数相等。

调整大小时,您还必须考虑之前的行数,并在调整大小时存储矩阵。

SEUV << boundary.EL

失败,因为矩阵 SEUV 已经调整大小并且行数多于 boundary.EL

SEUV << SEUV, boundary.EL

失败,因为 SEUV 占用了它自己的所有 space,没有 space 留给 boundary.EL

这里是一个示例,您可以如何附加 2 个具有 3 列和不同行的矩阵:

MatrixXi A(3,3);
A << 1, 1, 1, 1, 1, 1, 1, 1, 1;
cout << A << "\n\n";

MatrixXi B(2,3) ;
B << 2, 2, 2, 2, 2, 2;
cout << B << "\n\n";

MatrixXi tmp = A;
A.resize(A.rows() + B.rows(), NoChange);
A << tmp, B;
cout << A << "\n";