Skip to content
Algo Trade Analytics Docs

matrix.diff() - Pine Script Function

The function returns a new matrix resulting from the subtraction between matrices id1 and id2, or of matrix id1 and an id2 scalar (a numerical value).

matrix.diff(id1, id2) → matrix<int>
NameTypeDescription
id1matrixMatrix to subtract from.

A new matrix object containing the difference between id2 and id1.

//@version=6
indicator("`matrix.diff()` Example 2")// For efficiency, execute this code only once.
if barstate.islastconfirmedhistory // Create a 2x3 matrix with values `4`.
var m1 = matrix.new<float>(2, 3, 4) // Create a new matrix containing the difference between the `m1` matrix and the "int" value `1`.
var m2 = matrix.diff(m1, 1) // Display using a table.
var t = table.new(position.top_right, 1, 2, color.green)
table.cell(t, 0, 0, "Difference between a matrix and a scalar:")
table.cell(t, 0, 1, str.tostring(m2))