Sidechair

matrix.copy()

Browse the reference

Returns a new matrix with the same shape and items, independent of the original.

Try it in the editor

Changing the copy leaves the source matrix unchanged. The copy is shallow for object items, which both matrices keep pointing at.

Without a copy, assigning one matrix variable to another only gives the same grid a second name.

Syntax

Pine Script
matrix.copy(id: any matrix type) -> matrix<>
matrix.copy() -> matrix<>

Arguments

NameTypeDefaultDescription
idany matrix typerequiredThe matrix to duplicate. The copy is independent of the original.

Returns

matrix<>: A new matrix with the same dimensions and items.

Example

Pine ScriptEdit a copyOpen in editor
//@version=6
indicator("matrix.copy: independent", overlay = false)
matrix<float> a = matrix.new<float>(2, 2, 1.0)
b = matrix.copy(a)
matrix.set(b, 0, 0, 9.0)
plot(matrix.get(a, 0, 0), "Original stays 1")

See also

Runs on Sidechair