CroCo
CroCo

Reputation: 5741

Removing zero rows from a Matrix (elegant way)

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

Upvotes: 6

Views: 3697

Answers (3)

sgvd
sgvd

Reputation: 3939

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

Upvotes: 6

leewz
leewz

Reputation: 3346

Save each nonempty row into a vector, then make a new matrix

vector<block> buffer; //not sure of the type name for the rows
VectorXd zero(3); //or appropriate comparable type
for(int i = 0; i < Z.rows(); i++){ //note: possibly a function call each time
    if(Z.row(i) != zero)
    //if((Z.row(i) != 0).any()) //broadcasting comparison?
        buffer.push_back(Z.row(i));
}
MatrixXd return_value(buffer.size(), 3);
for(int i = buffer.size(); i --> 0;)
    return_value.row(i) = buffer[i];

return return_value;

Warning: Resizing the old one instead of making a new one may erase the contents before you can save them.

I can't read the docs from here, so you will have to see for yourself what kind of comparison operations you can do on block objects. As a last result, try row.any() (faster?) or row.squaredNorm() == 0.

Upvotes: 0

Luca Davanzo
Luca Davanzo

Reputation: 21520

Basically you can follow a pseudocode like this:

  • get N = rows, M = columns
  • iterate by each N
  • if N[0] = 0 iterate on rows exiting on first non-zero
  • if N[0] = 0 && .. && N[M] = 0
  • remove row

For removing single row:

void removeRow(Eigen::MatrixXd& matrix, unsigned int rowToRemove) {
    unsigned int numRows = matrix.rows() - 1;
    unsigned int numCols = matrix.cols();
    unsigned int rowPos = numRows - rowToRemove;
    if( rowToRemove < numRows ) {
        matrix.block(rowToRemove, 0, rowPos, numCols) = matrix.block(rowToRemove + 1, 0, rowPos,numCols);
    }
    matrix.conservativeResize(numRows, numCols);
}

Upvotes: 0

Related Questions