先构造一个简单的矩阵求和:
#include
int main(int argc,char** argv){
//申明一个4x4的int矩阵
Eigen::Matrix<int,4,4> warped_eigen;
//赋值
warped_eigen<<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16;
//打印出来看看
std::cout<<"warped_eigen:"<<std::endl<<warped_eigen<<std::endl;
//矩阵求和
int sum_m = warped_eigen.sum();
//打印结果
std::cout<<"sum of matrix: "<<std::endl<<sum_m<<std::endl;
return 0;
}
输出结果:
warped_eigen:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
sum of matrix:
136
#include
int main(int argc,char** argv){
//申明一个4x4的int矩阵
Eigen::Matrix<int,4,4> warped_eigen;
//赋值
warped_eigen<<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16;
//打印出来看看
std::cout<<"warped_eigen:"<<std::endl<<warped_eigen<<std::endl;
//矩阵求和
int sum_m = warped_eigen.sum();
//打印结果
std::cout<<"sum of matrix: "<<std::endl<<sum_m<<std::endl;
//按列求和
Eigen::VectorXi histogram1 = warped_eigen.colwise().sum();
std::cout<<"histogram1"<<std::endl<<histogram1<<std::endl;
return 0;
}
输出结果:
warped_eigen
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
sum of matrix:
136
histogram1
28
32
36
40
#include
int main(int argc,char** argv){
//申明一个4x4的int矩阵
Eigen::Matrix<int,4,4> warped_eigen;
//赋值
warped_eigen<<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16;
//打印出来看看
std::cout<<"warped_eigen:"<<std::endl<<warped_eigen<<std::endl;
//矩阵求和
int sum_m = warped_eigen.sum();
//打印结果
std::cout<<"sum of matrix: "<<std::endl<<sum_m<<std::endl;
//按列求和
Eigen::VectorXi histogram1 = warped_eigen.colwise().sum();
std::cout<<"histogram1"<<std::endl<<histogram1<<std::endl;
Eigen::MatrixXd::Index maxIndex;
int max_h = histogram1.maxCoeff(&maxIndex);
std::cout<<"histogram1 max is:"<<max_h<<" the max index is :"<<maxIndex<<std::endl;
return 0;
}
输出结果:
warped_eigen
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
sum of matrix:
136
histogram1
28
32
36
40
histogram1 max is:40 the max index is :3
REF:https://blog.csdn.net/Lo_Bamboo/article/details/105110174