Sidechair

matrix.median()

Browse the reference

Returns the middle value of all items in a matrix once they are sorted.

Try it in the editor

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

Pine Script
matrix.median(id: matrix<int/float>) -> series float
matrix.median(id: matrix<int>) -> series int
matrix.median() -> series float
matrix.median() -> series int

Arguments

NameTypeDefaultDescription
idmatrix<int/float> or matrix<int>requiredThe 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

Pine ScriptMedian of a gridOpen in editor
//@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