Sidechair

matrix.new()

Browse the reference

Creates a matrix with a given number of rows and columns, every cell holding the same starting value.

Try it in the editor

matrix.new<float>(3, 4, 0.0) is a three-row, four-column grid of zeros. The type goes in angle brackets and can be int, float, bool, string, color or an object type. Without dimensions the matrix starts empty, and rows and columns can then be added.

Cells left without a starting value are na. The total number of elements may not pass 100,000.

Syntax

Pine Script
matrix.new(rows: series int (optional), columns: series int (optional), initial_value: <matrix_type> (optional)) -> matrix<type>

Arguments

NameTypeDefaultDescription
rowsseries int0Number of rows in the new matrix. 0 gives a matrix with no rows.
columnsseries int0Number of columns in the new matrix. 0 gives a matrix with no columns.
initial_value<matrix_type>naThe value every cell starts with.

Returns

matrix<type>: A new matrix of the requested type.

Example

Pine ScriptIdentity matrixOpen in editor
//@version=6
indicator("matrix.new: identity", overlay = false)
matrix<float> id = matrix.new<float>(3, 3, 0.0)
for i = 0 to 2
    matrix.set(id, i, i, 1.0)
plot(matrix.trace(id), "Trace")

See also

Runs on Sidechair