Boost logo

Boost Users :

Subject: Re: [Boost-users] statistics: exact median, first and third quartile
From: gast128 (gast128_at_[hidden])
Date: 2013-03-01 11:31:08


> > Thx, but are you sure? Median is defined as the middle element in odd
> > distributions but is the average of the 2 middle elements in even
> > distributions.
>
> So? For a collection c of size 2n, you would use:
>
> nth_element( begin(c), begin(c) + n - 1, end(c) );
> nth_element( begin(c), begin(c) + n, end(c) );
> auto median = (c[n - 1] + c[n]) / 2;

Yes however nth_element has some overhead so doing it twice is not advisable.
Ofc u can fix that (by either partial sort etc.), but the best thing was:

auto med = boost::median(itBegin, itEnd); (//or std::)


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net