Questions tagged [median]

The median is the 'middle' value from a set of values. If the number of values is an even number, the median is the mean of the 'middle' values.

The median is generally used in programming in the sense of the term that comes from statistics. In simple terms it means the number that has half the values above it and half the values below it in the range. It is different than average in that an average can be influenced by extremes on either end of the spectrum. If a few numbers are a lot greater or a lot smaller than the rest, the average will be significantly different from the median. Median can give you a better sense of where the typical middle case is than the average.

The median is also very useful in signal and image processing, in the context of a moving median filter. This filter is usually used to reduce "salt and pepper" type noise, as well as spikes, because each output pixel or element contains the median value of the m-by-n neighborhood around that corresponding pixel in the data.

For a more statistically and technically correct and thorough explanation, see Wikipedia.

In scientific software for statistical computing and graphics, the median of a numeric vector can be found by function median, or quantile with prob = 0.5.

1286 questions
241
votes
33 answers

Function to Calculate Median in SQL Server

According to MSDN, Median is not available as an aggregate function in Transact-SQL. However, I would like to find out whether it is possible to create this functionality (using the Create Aggregate function, user defined function, or some other…
Yaakov Ellis
  • 38,048
  • 29
  • 119
  • 167
232
votes
9 answers

Find running median from a stream of integers

Possible Duplicate: Rolling median algorithm in C Given that integers are read from a data stream. Find median of elements read so far in efficient way. Solution I have read: We can use a max heap on left side to represent elements that are…
Luv
  • 4,873
  • 8
  • 42
  • 57
221
votes
40 answers

Simple way to calculate median with MySQL

What's the simplest (and hopefully not too slow) way to calculate the median with MySQL? I've used AVG(x) for finding the mean, but I'm having a hard time finding a simple way of calculating the median. For now, I'm returning all the rows to PHP,…
davr
  • 18,038
  • 16
  • 73
  • 98
210
votes
27 answers

Finding median of list in Python

How do you find the median of a list in Python? The list can be of any size and the numbers are not guaranteed to be in any particular order. If the list contains an even number of elements, the function should return the average of the middle…
ChucksPlace
  • 2,139
  • 2
  • 10
  • 3
121
votes
13 answers

Rolling median algorithm in C

I am currently working on an algorithm to implement a rolling median filter (analogous to a rolling mean filter) in C. From my search of the literature, there appear to be two reasonably efficient ways to do it. The first is to sort the initial…
AWB
  • 1,383
  • 2
  • 9
  • 8
87
votes
13 answers

"On-line" (iterator) algorithms for estimating statistical median, mode, skewness, kurtosis?

Is there an algorithm to estimate the median, mode, skewness, and/or kurtosis of set of values, but that does NOT require storing all the values in memory at once? I'd like to calculate the basic statistics: mean: arithmetic average variance: …
Ryan B. Lynch
  • 2,134
  • 3
  • 20
  • 20
73
votes
4 answers

How to find median and quantiles using Spark

How can I find median of an RDD of integers using a distributed method, IPython, and Spark? The RDD is approximately 700,000 elements and therefore too large to collect and find the median. This question is similar to this question. However, the…
pr338
  • 7,310
  • 14
  • 45
  • 64
61
votes
12 answers

Calculate median in c#

I need to write function that will accept array of decimals and it will find the median. Is there a function in the .net Math library?
WingMan20-10
  • 2,894
  • 8
  • 28
  • 41
59
votes
9 answers

Finding the median of an unsorted array

To find the median of an unsorted array, we can make a min-heap in O(nlogn) time for n elements, and then we can extract one by one n/2 elements to get the median. But this approach would take O(nlogn) time. Can we do the same by some method in O(n)…
Luv
  • 4,873
  • 8
  • 42
  • 57
52
votes
7 answers

How do I find the median of numbers in linear time using heaps?

Wikipedia says: Selection algorithms: Finding the min, max, both the min and max, median, or even the k-th largest element can be done in linear time using heaps. All it says is that it can be done, and not how. Can you give me some start on how…
Lazer
  • 79,569
  • 109
  • 264
  • 349
51
votes
10 answers

What is the right approach when using STL container for median calculation?

Let's say I need to retrieve the median from a sequence of 1000000 random numeric values. If using anything but std::list, I have no (built-in) way to sort sequence for median calculation. If using std::list, I can't randomly access values to…
sharkin
  • 11,386
  • 19
  • 83
  • 119
51
votes
11 answers

Calculating Median in Ruby

How do I calculate the median of an array of numbers using Ruby? I am a beginner and within the progress of my learning I am trying to stick to what has already been taught. Thus the other questions that I've found are beyond my scope. Here are my…
tomgalpin
  • 812
  • 1
  • 7
  • 17
48
votes
25 answers

Fastest way of finding the middle value of a triple?

Given is an array of three numeric values and I'd like to know the middle value of the three. The question is, what is the fastest way of finding the middle of the three? My approach is this kind of pattern - as there are three numbers there are six…
Gnark
  • 3,690
  • 7
  • 30
  • 44
45
votes
4 answers

Find the median of the sum of the arrays

Two sorted arrays of length n are given and the question is to find, in O(n) time, the median of their sum array, which contains all the possible pairwise sums between every element of array A and every element of array B. For instance: Let A[2,4,6]…
Aditya
  • 451
  • 5
  • 8
44
votes
10 answers

How to calculate or approximate the median of a list without storing the list

I'm trying to calculate the median of a set of values, but I don't want to store all the values as that could blow memory requirements. Is there a way of calculating or approximating the median without storing and sorting all the individual…
Nick Randell
  • 16,403
  • 17
  • 57
  • 67
1
2 3
85 86