在本征稀疏矩阵中将行/列/块设置为0? [英] Set row/column/block to 0 in Eigen sparse matrix?

查看:115
本文介绍了在本征稀疏矩阵中将行/列/块设置为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到使用新的Eigen 3.2,您可以从稀疏矩阵中获取行,列甚至块,是否可以将其中的任何一个设置为0?

I see with new Eigen 3.2, you can get row, column or even block from a sparse matrix, is there a way to set any of these to 0?

Eigen::SparseMatrix<float, Eigen::RowMajor> A( 5, 5 );
    A.block(1, 1, 2, 2) = 0; // won't work
    A.row(1) = 0; // won't work
    A.col(1) = 0; // won't work

谢谢!

推荐答案

对于5x5矩阵,使用稀疏矩阵是过大的选择。最好使用 MatrixXd ,甚至使用 Matrix< float,5,5> 。在这种情况下,可以使用 A.row(1).setZero()将行设置为零。稀疏矩阵对于大小约为1000x1000或更大的矩阵值得。

For 5x5 matrices, it is overkill to use a sparse matrix. Better use a MatrixXd, or even a Matrix<float,5,5>. In this case you can set a row to zero with A.row(1).setZero(). Sparse matrices are worth it for matrices of size about 1000x1000 and greater.

无论如何,一次抑制稀疏矩阵的多列和多行的最佳方法是使用修剪方法。下面是删除第二行和第三列的示例:

Anyway, the best to suppress multiple columns and rows of a sparse matrix at once is to use the prune method. Here is an example removing the second row and third column:

#include <Eigen/Sparse>
#include <iostream>

using namespace Eigen;

int main()
{
  Eigen::SparseMatrix<float, Eigen::RowMajor> A;
  A = MatrixXf::Random(5,5).sparseView();
  A.prune([](int i, int j, float) { return i!=1 && j!=2; });
  std::cout << A << "\n";
}

这篇关于在本征稀疏矩阵中将行/列/块设置为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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