Skip to content

Instantly share code, notes, and snippets.

@acple acple/Median.cs
Created Dec 14, 2014

Embed
What would you like to do?
//http://msdn.microsoft.com/ja-jp/library/cc981895.aspx
public static class Extensions
{
public static double Median(this IEnumerable<double> source)
{
var count = source.Count();
if (count == 0) throw new InvalidOperationException("Cannot compute median for an empty set.");
return source.OrderBy(x => x).Take((count / 2) + 1).Skip((count - 1) / 2).Average();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.