6

我有一个零行的矩阵。我想删除零行。矩阵为 Nx3。我所做的很简单。我创建std::vector其中每三个元素代表一行,然后将其转换为Eigen::MatrixXd. 有没有一种优雅的方法来删除零行?

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

3 回答 3

6

这是一个我觉得非常优雅的完整实现。请注意,这不保留非零规则的顺序,这可能不是您想要的,但在复杂性和代码行方面都更有效:

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());
}
于 2014-12-08T18:14:40.543 回答
0

将每个非空行保存到一个向量中,然后创建一个新矩阵

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;

警告:调整旧文件的大小而不是制作新文件可能会在保存之前删除内容。

我无法从这里阅读文档,因此您必须自己查看可以对块对象执行哪种比较操作。作为最后的结果,尝试row.any()(更快?)或row.squaredNorm() == 0.

于 2014-08-04T09:26:28.073 回答
0

基本上你可以遵循这样的伪代码:

  • 得到 N = 行,M = 列
  • 迭代每个 N
  • 如果 N[0] = 0 迭代第一个非零退出的行
  • 如果 N[0] = 0 && .. && N[M] = 0
  • 删除行

对于删除单行:

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);
}
于 2014-08-04T08:55:00.667 回答