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
43
votes
3 answers

O(n) algorithm to find the median of a collection of numbers

Problem: input is a (not necessarily sorted) sequence S = k1, k2, ..., kn of n arbitrary numbers. Consider the collection C of n² numbers of the form min{ki,kj}, for 1 <=i, j<=n. Present an O(n) time and O(n) space algorithm to find the median of…
ejf071189
  • 593
  • 1
  • 5
  • 8
38
votes
1 answer

scala median implementation

What's a fast implementation of median in scala? This is what I found on rosetta code: def median(s: Seq[Double]) = { val (lower, upper) = s.sortWith(_<_).splitAt(s.size / 2) if (s.size % 2 == 0) (lower.last + upper.head) / 2.0 else…
dsg
  • 12,504
  • 19
  • 64
  • 109
37
votes
6 answers

Compute Median of Values Stored In Vector - C++?

I'm a programming student, and for a project I'm working on, on of the things I have to do is compute the median value of a vector of int values. I'm to do this using only the sort function from the STL and vector member functions such as .begin(),…
Alex
  • 58,815
  • 45
  • 146
  • 176
35
votes
1 answer

Why does median trip up data.table (integer versus double)?

I have a data.table called enc.per.day for encounters per day. It has 2403 rows in which a date of service is specified and the number of patients seen on that day. I wanted to see the median number of patients seen on any type of…
Farrel
  • 9,584
  • 19
  • 57
  • 95
33
votes
10 answers

Map each list value to its corresponding percentile

I'd like to create a function that takes a (sorted) list as its argument and outputs a list containing each element's corresponding percentile. For example, fn([1,2,3,4,17]) returns [0.0, 0.25, 0.50, 0.75, 1.00]. Can anyone please either: Help me…
Jubbles
  • 3,722
  • 8
  • 29
  • 46
30
votes
5 answers

How can I calculate the median of values in SQLite?

I'd like to calculate the median value in a numeric row. How can I do that in SQLite 4?
mafu
  • 28,708
  • 38
  • 138
  • 232
29
votes
10 answers

How do I calculate the "median of five" in C#?

The median of five is sometimes used as an exercise in algorithm design and is known to be computable using only 6 comparisons. What is the best way to implement this "median of five using 6 comparisons" in C# ? All of my attempts seem to result in…
Gant
  • 28,894
  • 6
  • 44
  • 63
26
votes
5 answers

Calculate the median in MongoDB aggregation framework

Is there a way to calculate the median using the MongoDB aggregation framework?
user3080286
  • 261
  • 1
  • 3
  • 3
26
votes
6 answers

C++ Efficiently Calculating a Running Median

Those of you that have read my previous questions know about my work at understanding and implementing quicksort and quickselect, as well as some other basic algorithms. Quickselect is used to calculate the kth smallest element in an unsorted list,…
Edge
  • 2,337
  • 5
  • 28
  • 49
24
votes
10 answers

Calculating median - javascript

I've been trying to calculate median but still I've got some mathematical issues I guess as I couldn't get the correct median value and couldn't figure out why. Here's the code; class StatsCollector { constructor() { this.inputNumber =…
Muhammed Bayram
  • 253
  • 1
  • 2
  • 5
21
votes
4 answers

Incremental median computation with max memory efficiency

I have a process that generates values and that I observe. When the process terminates, I want to compute the median of those values. If I had to compute the mean, I could just store the sum and the number of generated values and thus have O(1)…
Mau
  • 13,256
  • 2
  • 28
  • 49
20
votes
3 answers

How to get median and quartiles/percentiles of an array in JavaScript (or PHP)?

This question is turned into a Q&A, because I had struggle finding the answer, and think it can be useful for others I have a JavaScript array of values and need to calculate in JavaScript its Q2 (50th percentile aka MEDIAN), Q1 (25th percentile)…
philippe
  • 1,577
  • 2
  • 17
  • 24
19
votes
2 answers

algorithm for nth_element

I have recently found out that there exists a method called nth_element in the STL. To quote the description: Nth_element is similar to partial_sort, in that it partially orders a range of elements: it arranges the range [first, last) such …
martinus
  • 16,710
  • 15
  • 65
  • 90
19
votes
9 answers

Minimum no. of comparisons to find median of 3 numbers

I was implementing quicksort and I wished to set the pivot to be the median or three numbers. The three numbers being the first element, the middle element, and the last element. Could I possibly find the median in less no. of comparisons?…
Karan Kalra
  • 413
  • 1
  • 4
  • 16
18
votes
5 answers

median of column with awk

How can I use AWK to compute the median of a column of numerical data? I can think of a simple algorithm but I can't seem to program it: What I have so far is: sort | awk 'END{print NR}' And this gives me the number of elements in the column. I'd…
Nick
  • 615
  • 2
  • 7
  • 9
1
2
3
85 86