Eigen使用笔记二十篇（五）：按行或按列求和的妙用（直方图）

矩阵求和

#include<Eigen/Core>
int main(int argc,char** argv){
//申明一个４x4的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;
return 0;
}


warped_eigen:
1  2  3  4
5  6  7  8
9 10 11 12
13 14 15 16

#include<Eigen/Core>
int main(int argc,char** argv){
//申明一个４x4的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<Eigen/Core>
int main(int argc,char** argv){
//申明一个４x4的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<Eigen/Core>
int main(int argc,char** argv){
//申明一个４x4的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


让问题更复杂一点

#include<Eigen/Core>
int main(int argc,char** argv){
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;

Eigen::VectorXi histogram1 = warped_eigen.colwise().sum();

std::cout<<"histogram1"<<std::endl<<histogram1<<std::endl;

Eigen::VectorXi histogram2 = warped_eigen.bottomRows(2).colwise().sum();

std::cout<<"warped_eigen b2"<<std::endl<<warped_eigen.bottomRows(2)<<std::endl;
std::cout<<"histogram2 b2"<<std::endl<<histogram2<<std::endl;

Eigen::MatrixXd::Index maxIndex_left, maxIndex_right;
histogram2.maxCoeff(&maxIndex_left);

//vec.segment(start, end - start + 1)
std::cout<<"histogram 0 1"<<std::endl<<histogram2.segment(0,2)<<std::endl;
std::cout<<"histogram 2 3"<<std::endl<<histogram2.segment(2,2)<<std::endl;

// histogram2.segment(2,3).maxCoeff(&maxIndex_right);

std::cout<<"max index"<<maxIndex_left<<std::endl;
return 0;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客