Sidechair

matrix.diff()

Browse the reference

Subtracts one matrix from another item by item, or subtracts one number from every item.

Try it in the editor

With two matrices of the same shape the result holds first minus second in each position. With a number, that number is taken off every item.

Order decides the sign: [[5, 5], [5, 5]] minus [[1, 2], [3, 4]] gives [[4, 3], [2, 1]], and the reverse gives the same numbers negated. Neither input changes; the answer is a new matrix. Subtracting a matrix from itself leaves all zeros, which matrix.is_zero() confirms.

Syntax

Pine Script
matrix.diff(id1: matrix<int>, id2: series int/float/matrix<int>) -> matrix<int>
matrix.diff(id1: matrix<int/float>, id2: series int/float/matrix<int/float>) -> matrix<float>
matrix.diff(id2: series int/float/matrix<int>) -> matrix<int>
matrix.diff(id2: series int/float/matrix<int/float>) -> matrix<float>

Arguments

NameTypeDefaultDescription
id1matrix<int> or matrix<int/float>requiredThe matrix subtracted from: each of its items has the matching item of id2, or the single number, taken away.
id2series int/float/matrix<int> or series int/float/matrix<int/float>requiredA matrix with exactly the rows and columns of id1, or one number subtracted from every item of id1.

Returns

  • matrix<int>: A new integer matrix holding first minus second in every position.
  • matrix<float>: A new float matrix holding first minus second in every position.
  • matrix<int>: A new integer matrix holding the receiving matrix minus id2.
  • matrix<float>: A new float matrix holding the receiving matrix minus id2.

Example

Pine ScriptResidual matrixOpen in editor
//@version=6
indicator("matrix.diff: residuals", overlay = false)
matrix<float> actual = matrix.new<float>(2, 2, 5.0)
matrix<float> model = matrix.new<float>(2, 2, 4.5)
res = matrix.diff(actual, model)
plot(matrix.get(res, 0, 0), "Residual")

See also

Runs on Sidechair