Sidechair

matrix.reshape()

Browse the reference

Changes the number of rows and columns of a matrix without changing its items.

Try it in the editor

The items are read row by row and laid out again in the new shape, so a 2 by 3 matrix can become 3 by 2 or 1 by 6. The new row count times the column count has to equal the element count.

Unlike matrix.transpose(), it keeps the reading order of the items rather than swapping rows and columns.

Syntax

Pine Script
matrix.reshape(id: any matrix type, rows: series int, columns: series int) -> void
matrix.reshape(rows: series int, columns: series int) -> void

Arguments

NameTypeDefaultDescription
idany matrix typerequiredThe matrix to give a new shape. The items keep their row-by-row order.
rowsseries intrequiredNumber of rows after the change. Rows times columns must equal the item count.
columnsseries intrequiredNumber of columns after the change. Rows times columns must equal the item count.

Returns

Nothing.

Example

Pine ScriptFlatten to one rowOpen in editor
//@version=6
indicator("matrix.reshape: flatten", overlay = false)
matrix<float> m = matrix.new<float>(2, 3, 1.0)
matrix.reshape(m, 1, 6)
plot(matrix.columns(m), "Columns now")

See also

Runs on Sidechair