matrix.new<type>() - Pine Script Function
matrix.new<type>()
Section titled “matrix.new<type>()”Overview
Section titled “Overview”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 ("
Syntax
Section titled “Syntax”matrix.new<type>(rows, columns, initial_value) → matrix<type>Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| rows | series int | Initial row count of the matrix. Optional. The default value is 0. |
Returns
Section titled “Returns”The ID of the new matrix object.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6indicator("`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 - 1for j = 0 to columns - 1matrix.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.islastconfirmedhistorylabel.new(bar_index, high, str.tostring(m))