0

Should I implement my own mean, std. deviation and median in C++, or there is already existing code I should use? I need to calculate these for a std::vector.

Thanks

user1486293
  • 79
  • 1
  • 2
  • 5
  • 1
    For the mean, use `std::accumulate / vec.size()`. – chris Jul 06 '12 at 03:02
  • 1
    For median, you can use [std::nth_element](http://www.cplusplus.com/reference/algorithm/nth_element/) (may have to run twice), or just [std:sort](http://www.cplusplus.com/reference/algorithm/sort/) the whole array and pick the median. – nhahtdh Jul 06 '12 at 03:10
  • 1
    Use [this code](http://www.java2s.com/Code/Cpp/STL-Algorithms-Helper/Computethesamplestandarddeviation.htm) for standard deviation. For median, use `std::nth_element` with the `n` set to the halfway point. – Sergey Kalinichenko Jul 06 '12 at 03:10
  • Almost identical question with several answers and references to further questions: http://stackoverflow.com/questions/7616511/calculate-mean-and-standard-deviation-from-a-vector-of-samples-in-c-using-boos – jogojapan Jul 06 '12 at 03:15

0 Answers0