Find the rostise maxCoeff and the maxCoeff index in Eigen

I want to find the maximum values ​​and indices on a row of a matrix. I based this on an example on my own website (Example 7).

#include <iostream> #include <Eigen/Dense> using namespace std; using namespace Eigen; int main() { MatrixXf mat(2,4); mat << 1, 2, 6, 9, 3, 1, 7, 2; MatrixXf::Index maxIndex; VectorXf maxVal = mat.rowwise().maxCoeff(&maxIndex); std::cout << "Maxima at positions " << endl; std::cout << maxIndex << std::endl; std::cout << "maxVal " << maxVal << endl; } 

The problem here is that my line

  VectorXf maxVal = mat.rowwise().maxCoeff(&maxIndex); 

wrong. Source example has

  float maxNorm = mat.rowwise().sum().maxCoeff(&maxIndex); 

i.e. There is an additional abbreviation .sum (). any suggestions? I assume that I just want my own equivalent to be what in matlab I would write as

 [maxval maxind] = max(mymatrix,[],2) 

i.e. find the maximum value and index by the second size mymatrix and return to the matrix (nrow (mymatrix), 2). thanks!

(also sent to its own list, sorry for cross-mailing.)

+6
source share
2 answers

I assume this is not possible without using a for loop using the current api. As you said yourself, you can get a vector of maximum row values ​​using

 VectorXf maxVal = mat.rowwise().maxCoeff(); 

As far as I can tell from the API documentation for maxCoeff () , it will only write one index value. The following code (untested) should provide you with what you want:

 MatrixXf::Index maxIndex[2]; VectorXf maxVal(2); for(int i=0;i<2;++i) maxVal(i) = mat.row(i).maxCoeff( &maxIndex[i] ); 
+9
source

Besides the Jakob "for loop" solution, you can also use libigl igl::mat_max , which functions like MATLAB row // max-max-max

 Eigen::MatrixXf mat(2,4); mat << 1, 2, 6, 9, 3, 1, 7, 2; Eigen::VectorXi maxIndices; Eigen::VectorXf maxVals; igl::mat_max(mat,2,maxVals,maxIndices); 

Then maxVals will contain [9;7] , and maxIndices will contain [3;2] .

+1
source

Source: https://habr.com/ru/post/920209/


All Articles