Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing zero rows from a Matrix (elegant way)

Tags:

c++

matrix

eigen3

I have a matrix with some zero rows. I would like to remove the zero rows. The matrix is Nx3. What I've done is simple. I create std::vector in which every three elements represent a row then I convert it to Eigen::MatrixXd. Is there an elegant way to remove the zero rows?

#include <iostream>
#include <vector>
#include <Eigen/Dense>



Eigen::MatrixXd VecToMat(const std::vector<double> vec)
{
    int rows(vec.size()/3) , cols(3);
    Eigen::MatrixXd temp( rows , cols);
    int count(0);
    for ( int i(0); i < rows; ++i)
    {
        temp(i,0) = vec[count]; 
        temp(i,1) = vec[count+1]; 
        temp(i,2) = vec[count+2]; 
        count += 3;
    }

    return temp;
}

Eigen::MatrixXd  getNewMat(Eigen::MatrixXd& Z)
{
    std::vector<double> vec;

    for ( int i(0); i < Z.rows(); ++i)
    {
        if ( (Z(i,0) && Z(i,1) && Z(i,2)) != 0 ){
            vec.push_back(Z(i,0));
            vec.push_back(Z(i,1));
            vec.push_back(Z(i,2));
        }
    }

    Eigen::MatrixXd temp = VecToMat(vec);

    return temp;
}

int main()
{
    Eigen::MatrixXd Z(5,3);
    Z.setOnes();


    Z(0,0) = 0;
    Z(0,1) = 0;
    Z(0,2) = 0;

    Z(1,0) = 0;
    Z(1,1) = 0;
    Z(1,2) = 0;

    Z(2,0) = 0;
    Z(2,1) = 0;
    Z(2,2) = 0;

    std::cout << Z << std::endl << std::endl;
    std::cout << getNewMat(Z) << std::endl;
    std::cin.get();
    return 0;
}
like image 355
CroCo Avatar asked Aug 04 '14 08:08

CroCo


People also ask

Can you remove zero row from Matrix?

To remove the rows of 0 , you can: sum the absolute value of each rows (to avoid having a zero sum from a mix of negative and positive numbers), which gives you a column vector of the row sums. keep the index of each line where the sum is non-zero.

How do you delete a column and a row in Matlab?

The easiest way to remove a row or column from a matrix is to set that row or column equal to a pair of empty square brackets [] . For example, create a 4-by-4 matrix and remove the second row.


1 Answers

Here is a full implementation I find quite elegant. Note that this one does not preserve order of non-zero rules, which maybe isn't what you want, but is more efficient both in complexity and lines of code:

void removeZeroRows(Eigen::MatrixXd& mat)
{
  Matrix<bool, Dynamic, 1> empty = (mat.array() == 0).rowwise().all();

  size_t last = mat.rows() - 1;
  for (size_t i = 0; i < last + 1;)
  {
    if (empty(i))
    {
      mat.row(i).swap(mat.row(last));
      empty.segment<1>(i).swap(empty.segment<1>(last));
      --last;
    }
    else
      ++i;
  }
  mat.conservativeResize(last + 1, mat.cols());
}
like image 56
sgvd Avatar answered Sep 25 '22 14:09

sgvd