Eigen库学习笔记(十五)Eigen获取最小值与最小值索引
准备编写NMS时,需要获取当前行最大值,下面用一段代码测试了类似功能。
#include<Eigen/Core>
#include<iostream>
using namespace std;
using namespace Eigen;
int main()
{
MatrixXd::Index maxRow, maxCol;
MatrixXd::Index minRow, minCol;
MatrixXd mMat(4,4);
mMat << 11, 10, 13, 15,
3, 24, 56, 1,
2, 12, 45, 0,
8, 5, 6, 4;
double min = mMat.minCoeff(&minRow,&minCol);
double max = mMat.maxCoeff(&maxRow,&maxCol);
cout << "Max = \n" << max << endl;
cout << "Min = \n" << min << endl;
cout << "minRow = " << minRow << "minCol = " <<minCol<<endl;
cout << "maxRow = " << maxRow << "maxCol = " << maxCol << endl;
Eigen::VectorXd v(4);
Eigen::VectorXd::Index col;
for(int i=0;i<=3;i++){
//TODO
v = mMat.row(i);
cout << "v = " << v <<endl;
double minv = v.minCoeff(&col);
cout << " min v = " << minv <<endl;
cout << "min v index = " << col <<endl;
}
return 0;
}
运行结果
Max =
56
Min =
0
minRow = 2minCol = 3
maxRow = 1maxCol = 2
v = 11
10
13
15
min v = 10
min v index = 1
v = 3
24
56
1
min v = 1
min v index = 3
v = 2
12
45
0
min v = 0
min v index = 3
v = 8
5
6
4
min v = 4
min v index = 3
--------------------------------
Process exited after 0.02429 seconds with return value 0
Press ANY key to exit...