Eigen Library: Setting all Non-Zero elements in a SparseMatrix *Row* to Zero

1.1k Views Asked by At

For a matrix A and row 'i' in MATLAB, I would do the following:

A(i,:) = zeros(size(A(i,:));

A stupid way to do the same would be to iterate through the whole row and setting the non-zero values to zero. This is not suitable as I am working with huge matrices (200,000+ columns) here.

Is there a simple and fast way to do this? I am using the SparseMatrix class in Eigen. I also know that there are at most 3 non zero values in each row. I don't know where.

I need this to edit a few rows in the matrix with new values. The idea is that I first make the whole row zero and then assign my values to certain elements on the same row.

The following question on StackOverflow was relevant but unfortunately had no answers.

1

There are 1 best solutions below

0
On

The equivalent of the Matlab code above can be implemented using the setZero function, as follows:

A.row(i).setZero();

Note that this works for dense matrices, not sparse ones. The MatrixXd class is recommended if you want the size to be dynamic.