级联稀疏矩阵本征 [英] Concatenate sparse matrix Eigen

查看:115
本文介绍了级联稀疏矩阵本征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eigen中有两个稀疏矩阵,我想将它们垂直合并为一个.例如,代码的目标是:

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<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;

我在论坛上找到了一些解决方案,我无法实现它.

I found some solutions on a forum however, I wasn't able to implement it.

垂直合并矩阵的正确方法是什么?

What's the proper way to join the matrices vertically?

修改

我的实现:

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;

出现类型错误,根据编译器,右侧是Eigen :: Block,左侧是Eigen :: SparseMatrix

I get an error of types, acording to the compiler the right hand side is an Eigen::Block and the left side is Eigen::SparseMatrix

推荐答案

据我所知,目前没有内置解决方案.通过使用

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<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();

这篇关于级联稀疏矩阵本征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆