Skip to content
Algo Trade Analytics Docs

matrix.new<type>() - Pine Script Function

The function creates a new matrix object. A matrix is a two-dimensional data structure containing rows and columns. All elements in the matrix must be of the type specified in the type template ("").

matrix.new<type>(rows, columns, initial_value) → matrix<type>
NameTypeDescription
rowsseries intInitial row count of the matrix. Optional. The default value is 0.

The ID of the new matrix object.

//@version=6
indicator("`matrix.new<type>()` Example 4")// Function to create a matrix with random values (0.0 to 1.0).matrixRandom(int rows, int columns)=> result = matrix.new<float>(rows, columns)
for i = 0 to rows - 1
for j = 0 to columns - 1
matrix.set(result, i, j, math.random())
result// Create a 2x3 matrix with random values.var m = matrixRandom(2, 3)// Display using a label.
if barstate.islastconfirmedhistory
label.new(bar_index, high, str.tostring(m))