mat_max.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef IGL_MAT_MAX_H
  2. #define IGL_MAT_MAX_H
  3. #include <Eigen/Dense>
  4. namespace igl
  5. {
  6. // Ideally this becomes a super overloaded function supporting everything
  7. // that matlab's max supports
  8. // Max function for matrices to act like matlab's max function. Specifically
  9. // like [Y,I] = max(X,[],dim);
  10. //
  11. // Templates:
  12. // T should be a eigen matrix primitive type like int or double
  13. // Inputs:
  14. // X m by n matrix
  15. // dim dimension along which to take max
  16. // Outputs:
  17. // Y n-long sparse vector (if dim == 1)
  18. // or
  19. // Y m-long sparse vector (if dim == 2)
  20. // I vector the same size as Y containing the indices along dim of maximum
  21. // entries
  22. template <typename T>
  23. inline void mat_max(
  24. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & X,
  25. const int dim,
  26. Eigen::Matrix<T,Eigen::Dynamic,1> & Y,
  27. Eigen::Matrix<int,Eigen::Dynamic,1> & I);
  28. }
  29. template <typename T>
  30. inline void igl::mat_max(
  31. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & X,
  32. const int dim,
  33. Eigen::Matrix<T,Eigen::Dynamic,1> & Y,
  34. Eigen::Matrix<int,Eigen::Dynamic,1> & I)
  35. {
  36. assert(dim==1||dim==2);
  37. // output size
  38. int n = (dim==1?X.cols():X.rows());
  39. // resize output
  40. Y.resize(n);
  41. I.resize(n);
  42. // loop over dimension opposite of dim
  43. for(int j = 0;j<n;j++)
  44. {
  45. int PHONY;
  46. int i;
  47. int m;
  48. if(dim==1)
  49. {
  50. m = X.col(j).maxCoeff(&i,&PHONY);
  51. }else
  52. {
  53. m = X.row(j).maxCoeff(&PHONY,&i);
  54. }
  55. Y(j) = m;
  56. I(j) = i;
  57. }
  58. }
  59. #endif