Skip to content
Algo Trade Analytics Docs

matrix.mode() - Pine Script Function

The function calculates the mode of the matrix, which is the most frequently occurring value from the matrix elements. When there are multiple values occurring equally frequently, the function returns the smallest of those values.

matrix.mode(id) → series float
NameTypeDescription
idmatrix<int/float>A matrix object.

The most frequently occurring value from the id matrix. If none exists, returns the smallest value instead.

  • Note that na elements of the matrix are not considered when calculating the mode.
//@version=6
indicator("`matrix.mode()` Example")// Create a 2x2 matrix.var m = matrix.new<int>(2, 2, na)// Fill the matrix with values.matrix.set(m, 0, 0, 0)
matrix.set(m, 0, 1, 0)
matrix.set(m, 1, 0, 1)
matrix.set(m, 1, 1, 1)// Get the mode of the matrix.var x = matrix.mode(m)
plot(x, 'Mode of the matrix')