matrix.mode() - Pine Script Function
matrix.mode()
Section titled “matrix.mode()”Overview
Section titled “Overview”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.
Syntax
Section titled “Syntax”matrix.mode(id) → series floatParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| id | matrix<int/float> | A matrix object. |
Returns
Section titled “Returns”The most frequently occurring value from the id matrix. If none exists, returns the smallest value instead.
Remarks
Section titled “Remarks”- Note that na elements of the matrix are not considered when calculating the mode.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6indicator("`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')