Concatenate sparse matrix Eigen

后端 未结 3 546
闹比i
闹比i 2021-01-21 13:13

I have two sparse matrices in Eigen, and I would like to join them vertically into one. As an example the target of the code would be:

SparseMatrix         


        
3条回答
  •  太阳男子
    2021-01-21 13:55

    As far as I know, there is currently no built-in solution. You can be way more efficient than your solution by using the internal insertBack function:

    SparseMatrix M(L.rows() + C.rows(), L.cols());
    M.reserve(L.nonZeros() + C.nonZeros());
    for(Index c=0; c::InnerIterator itL(L, c); itL; ++itL)
             M.insertBack(itL.row(), c) = itL.value();
        for(SparseMatrix::InnerIterator itC(C, c); itC; ++itC)
             M.insertBack(itC.row()+L.rows(), c) = itC.value();
    }
    M.finalize();
    

提交回复
热议问题