Returns the middle value of all items in a matrix once they are sorted.
With an odd number of items it is the center one; with an even number it is the mean of the two center ones. The shape of the matrix does not matter.
It resists outliers better than matrix.avg(): [[1, 2], [3, 100]] has a median of 2.5 but an average of 26.5.
Syntax
matrix.median(id: matrix<int/float>) -> series float
matrix.median(id: matrix<int>) -> series int
matrix.median() -> series float
matrix.median() -> series intArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | matrix<int/ | required | The matrix whose items are sorted together to find the middle value, whatever its shape. |
Returns
series float: The middle value as a float; for an even count it is the mean of the two center items.series int: The middle value typed as a whole number, to match an integer matrix.series float: The middle value of the receiving matrix as a float.series int: The middle value of the receiving matrix typed as a whole number.
Example
//@version=6
indicator("matrix.median: middle", overlay = false)
matrix<float> m = matrix.new<float>(1, 3, 1.0)
matrix.set(m, 0, 2, 100.0)
plot(matrix.median(m), "Median")See also
Runs on Sidechair