Sidechair

matrix.avg()

Browse the reference

Returns the mean of all items in a matrix.

Try it in the editor

Every cell counts once, whatever its row or column, and the result is the total divided by the number of items. The matrix [[1, 2], [3, 4]] averages to 2.5.

For a row or column on its own, take it with matrix.row() or matrix.col() and use array.avg().

Syntax

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

Arguments

NameTypeDefaultDescription
idmatrix<int/float> or matrix<int>requiredThe matrix whose items are all averaged together, whatever its shape.

Returns

  • series float: The mean of every item as a float.
  • series int: The mean typed as a whole number, to match an integer matrix; a decimal-valued matrix keeps the fraction.
  • series float: The mean of every item in the receiving matrix as a float.
  • series int: The mean of the receiving matrix typed as a whole number.

Example

Pine ScriptMean of a gridOpen in editor
//@version=6
indicator("matrix.avg: grid mean", overlay = false)
matrix<float> m = matrix.new<float>(2, 2, 3.0)
matrix.set(m, 0, 0, 7.0)
plot(matrix.avg(m), "Mean")

See also

Runs on Sidechair