Removing zero columns or rows using eigen

老子叫甜甜 提交于 2019-12-10 19:15:11

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/41305178/removing-zero-columns-or-rows-using-eigen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!