Browse Source

fix median for even input

Former-commit-id: 3437da841c68c21fb83ce3c8f754d9b3641c972f
Alec Jacobson 11 years ago
parent
commit
a8a2ca6d03
1 changed files with 8 additions and 1 deletions
  1. 8 1
      include/igl/median.cpp

+ 8 - 1
include/igl/median.cpp

@@ -24,6 +24,13 @@ IGL_INLINE bool igl::median(const Eigen::VectorXd & V, double & m)
   // http://stackoverflow.com/a/1719155/148668
   // http://stackoverflow.com/a/1719155/148668
   size_t n = vV.size()/2;
   size_t n = vV.size()/2;
   nth_element(vV.begin(),vV.begin()+n,vV.end());
   nth_element(vV.begin(),vV.begin()+n,vV.end());
-  m = vV[n];
+  if(vV.size()%2==0)
+  {
+    nth_element(vV.begin(),vV.begin()+n-1,vV.end());
+    m = 0.5*(vV[n]+vV[n-1]);
+  }else
+  {
+    m = vV[n];
+  }
   return true;
   return true;
 }
 }