Sidechair

matrix.swap_columns()

Browse the reference

Exchanges two columns of a matrix in place.

Try it in the editor

The two columns trade items; all other columns stay put. Use it to reorder variables in a table of observations.

A column swapped with itself is unchanged.

Syntax

Pine Script
matrix.swap_columns(id: any matrix type, column1: series int, column2: series int) -> void
matrix.swap_columns(column1: series int, column2: series int) -> void

Arguments

NameTypeDefaultDescription
idany matrix typerequiredThe matrix whose two columns trade places in place.
column1series intrequiredIndex of the first column.
column2series intrequiredIndex of the second column.

Returns

Nothing.

Example

Pine ScriptSwap the two columnsOpen in editor
//@version=6
indicator("matrix.swap_columns: swap", overlay = false)
matrix<float> m = matrix.new<float>(2, 2, 0.0)
matrix.set(m, 0, 0, 1.0)
matrix.set(m, 0, 1, 2.0)
matrix.swap_columns(m, 0, 1)
plot(matrix.get(m, 0, 0), "Now 2")

See also

Runs on Sidechair