Sidechair

matrix.mode()

Browse the reference

Returns the value that occurs most often in a matrix.

Try it in the editor

Items are compared exactly, so the function suits whole numbers and rounded values. If several values tie for most frequent, the smallest of them is returned.

The position of an item in the grid is ignored: [[1, 2], [2, 3]] gives 2, and in [[1, 2], [3, 4]] every value ties so the smallest, 1, wins.

Syntax

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

Arguments

NameTypeDefaultDescription
idmatrix<int/float> or matrix<int>requiredThe matrix whose items are tallied to find the most frequent value; where an item sits does not matter.

Returns

  • series float: The most frequent value as a float; ties go to the smallest.
  • series int: The most frequent value as a whole number, to match an integer matrix.
  • series float: The most frequent value in the receiving matrix as a float.
  • series int: The most frequent value in the receiving matrix as a whole number.

Example

Pine ScriptMost common cell valueOpen in editor
//@version=6
indicator("matrix.mode: common value", overlay = false)
matrix<float> m = matrix.new<float>(2, 2, 3.0)
matrix.set(m, 0, 0, 9.0)
plot(matrix.mode(m), "Mode")

See also

Runs on Sidechair