median.cpp 447 B

12345678910111213141516171819202122
  1. #include "median.h"
  2. #include "matrix_to_list.h"
  3. #include <vector>
  4. #include <algorithm>
  5. IGL_INLINE bool igl::median(const Eigen::VectorXd & V, double & m)
  6. {
  7. using namespace std;
  8. using namespace igl;
  9. if(V.size() == 0)
  10. {
  11. return false;
  12. }
  13. vector<double> vV;
  14. matrix_to_list(V,vV);
  15. // http://stackoverflow.com/a/1719155/148668
  16. size_t n = vV.size()/2;
  17. nth_element(vV.begin(),vV.begin()+n,vV.end());
  18. m = vV[n];
  19. return true;
  20. }