首先,我不确定这是否可能。我想检查特征库中的矩阵是否为零(注意:我必须声明它)。我的解决方案是检查所有元素是否为零。我的问题是在保持矩阵大小不变的情况下,是否有另一种方法来完成这项任务?
#include <iostream>
#include <Eigen/Dense>
// true if it is empty, false if not
bool isEmpty(Eigen::MatrixXd& Z)
{
bool check = true;
for (int row(0); row < Z.rows(); ++row)
for (int col(0); col < Z.cols(); ++col){
if ( Z(row,col) != 0 ){
check = false;
break;
}
}
return check;
}
int main()
{
Eigen::MatrixXd Z(3,3);
if ( isEmpty(Z) )
std::cout << Z.size() << std::endl;
else
Z.setZero(0,0); // resize the matrix (not clever way I know)
std::cin.get();
return 0;
}