Sidechair

matrix.is_binary()

Browse the reference

Returns true when every item in a matrix is either 0 or 1.

Try it in the editor

Every item must be exactly 0 or 1: [[1, 0], [0, 1]] passes and [[1, 0.5], [0, 1]] fails because of the 0.5.

Adjacency matrices, masks and flag grids are binary. It is a quick sanity check before treating a grid as a set of yes and no answers.

Syntax

Pine Script
matrix.is_binary(id: matrix<int/float>) -> series bool
matrix.is_binary() -> series bool

Arguments

NameTypeDefaultDescription
idmatrix<int/float>requiredThe matrix whose every item must be 0 or 1 for the test to pass; any other number fails it.

Returns

  • series bool: true when every item is 0 or 1, otherwise false.
  • series bool: true when the receiving matrix holds only 0 and 1, otherwise false.

Example

Pine ScriptFlag grid checkOpen in editor
//@version=6
indicator("matrix.is_binary: flags", overlay = false)
matrix<int> flags = matrix.new<int>(2, 2, 0)
matrix.set(flags, 0, 1, 1)
plot(matrix.is_binary(flags) ? 1 : 0, "Binary")

See also

Runs on Sidechair