I want to find the maximum values and indices by row of a matrix. I based this on an example on the eigen 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;
}
Problem here is that my line
VectorXf maxVal = mat.rowwise().maxCoeff(&maxIndex);
is wrong. The original example has
float maxNorm = mat.rowwise().sum().maxCoeff(&maxIndex);
i.e. there is an additional reduction .sum() involved. any suggestions? I guess I just want the eigen equivalent to what in matlab I would write as
[maxval maxind] = max(mymatrix,[],2)
i.e. find maximum value and it's index over the second dimension of mymatrix and return in a (nrow(mymatrix),2) matrix. thanks!
(sent to the eigen list as well, sorry for cross-posting.)