#ifndef MEDIAN_H #define MEDIAN_H // This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2013 Alec Jacobson // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include #include namespace igl { template void matrix_to_list(const Eigen::DenseBase& M, std::vector &V) { using namespace std; V.resize(M.size()); // loop over cols then rows for(int j =0; j bool median( const Eigen::MatrixBase & V, mType & m) { using namespace std; if(V.size() == 0) { return false; } vector vV; matrix_to_list(V,vV); // http://stackoverflow.com/a/1719155/148668 size_t n = vV.size()/2; nth_element(vV.begin(),vV.begin()+n,vV.end()); 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; } } #endif