Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing zero columns or rows using eigen

Tags:

c++

eigen

I was wondering if there is a more efficient way to remove columns or rows that are all zero elements. I am sure there is using the functions in the eigen library but I do not know how.

Right now I am doing it like so, with the idea of the while loop being used in case there are multiple rows/columns that sum to zero I dont want to exceed range limits or pass any zero rows.

void removeZeroRows() {
    int16_t index = 0;
    int16_t num_rows = rows();

    while (index < num_rows) {
        double sum = row(index).sum();
        // I use a better test if zero but use this for demonstration purposes 
        if (sum == 0.0) {
            removeRow(index);
        }
        else {
            index++;
        }

        num_rows = rows();
    }
}
like image 394
user2840470 Avatar asked Sep 29 '26 03:09

user2840470


1 Answers

Currently (Eigen 3.3), there is no direct functionality for this (though it is planned for Eigen 3.4).

Meanwhile can use something like this (of course, row and col can be interchanged, and output is just for illustration):

Eigen::MatrixXd A;
A.setRandom(4,4);
A.col(2).setZero();

// find non-zero columns:
Eigen::Matrix<bool, 1, Eigen::Dynamic> non_zeros = A.cast<bool>().colwise().any();

std::cout << "A:\n" << A << "\nnon_zeros:\n" << non_zeros << "\n\n";

// allocate result matrix:
Eigen::MatrixXd res(A.rows(), non_zeros.count());

// fill result matrix:
Eigen::Index j=0;
for(Eigen::Index i=0; i<A.cols(); ++i)
{
    if(non_zeros(i))
        res.col(j++) = A.col(i);
}

std::cout << "res:\n" << res << "\n\n";

Generally, you should avoid resizing a matrix at every iteration, but resize it to the final size as soon as possible.

With Eigen 3.4 something similar to this will be possible (syntax is not final yet):

Eigen::MatrixXd res = A("", A.cast<bool>().colwise().any());

Which would be equivalent to Matlab/Octave:

res = A(:, any(A));
like image 74
chtz Avatar answered Oct 01 '26 20:10

chtz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!