title: Removing zero rows from a Matrix (elegant way)
body: <p>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 <code>std::vector</code> in which every three elements represent a row  then I convert it to <code>Eigen::MatrixXd</code>. Is there an elegant way to remove the zero rows?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;Eigen/Dense&gt;
  
  
  
  Eigen::MatrixXd VecToMat(const std::vector&lt;double&gt; vec)
  {
      int rows(vec.size()/3) , cols(3);
      Eigen::MatrixXd temp( rows , cols);
      int count(0);
      for ( int i(0); i &lt; 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&amp; Z)
  {
      std::vector&lt;double&gt; vec;
  
      for ( int i(0); i &lt; Z.rows(); ++i)
      {
          if ( (Z(i,0) &amp;&amp; Z(i,1) &amp;&amp; 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 &lt;&lt; Z &lt;&lt; std::endl &lt;&lt; std::endl;
      std::cout &lt;&lt; getNewMat(Z) &lt;&lt; std::endl;
      std::cin.get();
      return 0;
  }
  </code></pre>