[英]How to add a block matrix onto a sparse matrix in Eigen
For example I have a 10x10 SparseMatrix A, and I want to add a 3x3 identity matrix to the upper left corner of A. 例如,我有一个10x10的SparseMatrix A,我想在A的左上角添加一个3x3的单位矩阵。
A is known to be already non-zero in those 3 entries. 在这3个条目中,已知A已经不为零。
If I have to add the values one by one it is ok too, but I didn't find the method to manipulate on elements of a Sparse Matrix in Eigen. 如果必须一一添加值,也可以,但是我没有找到在Eigen中对稀疏矩阵的元素进行操作的方法。
Did I miss something? 我错过了什么?
If all you want is to apply an operation to a specific element at a time, you can use coeffRef
like so: 如果只想一次将操作应用于特定元素,则可以像下面这样使用
coeffRef
:
typedef Eigen::Triplet<double> T;
std::vector<T> coefficients;
for (int i = 0; i < 9; i++) coefficients.push_back(T(i, i, (i % 3) + 1));
Eigen::SparseMatrix<double> A(10, 10);
A.setFromTriplets(coefficients.begin(), coefficients.end());
std::cout << A << "\n\n";
for (int i = 0; i < 3; i++) A.coeffRef(i,i) += 1;
std::cout << A << "\n\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.