Sidechair

matrix.swap_rows()

Browse the reference

Exchanges two rows of a matrix in place.

Try it in the editor

The items of the two rows trade places and every other row stays where it is. Swapping a row with itself changes nothing.

Row operations such as pivoting in elimination are built from swaps like this.

Syntax

Pine Script
matrix.swap_rows(id: any matrix type, row1: series int, row2: series int) -> void
matrix.swap_rows(row1: series int, row2: series int) -> void

Arguments

NameTypeDefaultDescription
idany matrix typerequiredThe matrix whose two rows trade places in place.
row1series intrequiredIndex of the first row to swap. Counts from 0.
row2series intrequiredIndex of the second row to swap. Counts from 0.

Returns

Nothing.

Example

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

See also

Runs on Sidechair