Jav*_*ier 2 c++ sparse-matrix eigen
我在 Eigen 中有两个稀疏矩阵,我想将它们垂直连接成一个。例如,代码的目标是:
SparseMatrix<double> matrix1;
matrix1.resize(10, 10);
SparseMatrix<double> matrix2;
matrix2.resize(5, 10);
SparseMatrix<double> MATRIX_JOIN;
MATRIX_JOIN.resize(15, 10);
MATRIX_JOIN << matrix1, matrix2;
Run Code Online (Sandbox Code Playgroud)
我在论坛上找到了一些解决方案,但是我无法实施。
垂直连接矩阵的正确方法是什么?
编辑
我的实现:
SparseMatrix<double> L;
SparseMatrix<double> C;
// ... (Operations with the matrices)
SparseMatrix<double> EMATRIX;
EMATRIX.resize(L.rows() + C.rows(), L.cols());
EMATRIX.middleRows(0, L.rows()) = L;
EMATRIX.middleRows(L.rows(), C.rows()) = C;
Run Code Online (Sandbox Code Playgroud)
我收到类型错误,根据编译器的说法,右侧是 Eigen::Block 而左侧是 Eigen::SparseMatrix
据我所知,目前没有内置的解决方案。通过使用内部insertBack函数,您可以比解决方案更高效:
SparseMatrix<double> M(L.rows() + C.rows(), L.cols());
M.reserve(L.nonZeros() + C.nonZeros());
for(Index c=0; c<L.cols(); ++c)
{
M.startVec(c); // Important: Must be called once for each column before inserting!
for(SparseMatrix<double>::InnerIterator itL(L, c); itL; ++itL)
M.insertBack(itL.row(), c) = itL.value();
for(SparseMatrix<double>::InnerIterator itC(C, c); itC; ++itC)
M.insertBack(itC.row()+L.rows(), c) = itC.value();
}
M.finalize();
Run Code Online (Sandbox Code Playgroud)