CODESCRIPT

Matrices

51 entries. What each one takes, what it returns, and a working example.

matrixAdd(a, b)

Element-wise addition (new matrix).

A new matrix with the cells at each position added together.

a
matrix
The first matrix.
b
matrix
The second matrix; must have the same size as a.

Note

The two matrices are added cell by cell. The result is a new matrix; the inputs are unchanged.

Limits

Both matrices must have the same row and column counts; a size mismatch raises an error.

CODESCRIPT
a = matrixNew(2, 2, 1)
b = matrixNew(2, 2, 2)
s = matrixAdd(a, b)
plot(matrixGet(s, 0, 0))

1+2 = 3 (every cell).

matrixAddCol(m, i?, array?)

Inserts a column at position i.

Returns nothing (na); it grows the matrix by one column in place.

m
matrix
The matrix to add a column to.
i
number
The insert position (optional); appended at the end if omitted. Clamped into the 0..cols range if out of range.
array
array
An array of the column values (optional); padded with 0 if shorter than the row count.

Note

Inserts the array as a column at the given position and increases the column count. If the matrix is empty (0 rows), the added column's length sets the row count.

CODESCRIPT
m = matrixNew(2, 1, 1)
matrixAddCol(m, 1, arrayFrom(7, 8))
plot(matrixGet(m, 0, 1))

First cell of the appended column is 7.

matrixAddRow(m, i?, array?)

Inserts a row at position i.

Returns nothing (na); it grows the matrix by one row in place.

m
matrix
The matrix to add a row to.
i
number
The insert position (optional); appended at the end if omitted. Clamped into the 0..rows range if out of range.
array
array
An array of the row values (optional); padded with 0 if shorter than the column count, truncated if longer.

Note

Inserts the array as a row at the given position and increases the row count. If the matrix is empty (0 columns), the first added row's length sets the column count.

When to use

To grow a matrix by adding rows bar by bar — an accumulating table of observations.

CODESCRIPT
m = matrixNew(1, 2, 1)
matrixAddRow(m, 1, arrayFrom(7, 8))
plot(matrixGet(m, 1, 0))

First cell of the appended row is 7.

matrixAvg(matrix)

Average of all elements.

The arithmetic mean of all cells; na if the matrix is empty or not a matrix.

matrix
matrix
The matrix to average.

Note

Divides the sum by the cell count. Returns na (not 0) on an empty matrix; guard the result with na().

CODESCRIPT
m = matrixNew(2, 2, 4)
plot(matrixAvg(m))

All cells 4 → mean 4.

matrixCol(matrix, j)

Returns the jth column as an array. Result: Arr(len=2, [1.0, 1.0])

An array holding the values of column j; an empty array if the index is out of range.

matrix
matrix
The matrix to take a column from.
j
number
Column index; 0 is the first column.

Note

Collects the column top to bottom into an array. The returned array is a copy; it does not affect the matrix.

When to use

To process all samples of a variable (a column) as an array — mean, max, standard deviation.

CODESCRIPT
m = matrixNew(3, 2, 5)
c = matrixCol(m, 1)
plot(arraySize(c))

Column 1 has 3 elements.

matrixCols(matrix)

Number of columns.

The number of columns in the matrix; 0 if it is not a matrix.

matrix
matrix
The matrix to measure.

Note

Used as the loop bound over columns. matrixColumns is a second name for the same thing.

CODESCRIPT
m = matrixNew(3, 2, 0)
plot(matrixCols(m))

2 columns.

matrixColumns(m)

Number of columns (same as matrixCols).

The number of columns in the matrix; 0 if it is not a matrix.

m
matrix
The matrix to measure.

Note

Returns exactly the same result as matrixCols — an alternative name provided for readability.

CODESCRIPT
m = matrixNew(3, 4, 0)
plot(matrixColumns(m))

4 columns.

matrixConcat(m1, m2)

Appends m2's rows below m1 — m1 grows and is returned.

A new matrix with m2's rows appended below m1's rows.

m1
matrix
The top matrix.
m2
matrix
The matrix to append below; its column count must match m1.

Note

Performs a vertical stack (appends rows below). The result is a new matrix; the inputs are unchanged.

Limits

Both matrices must have the same column count; a mismatch raises an error.

CODESCRIPT
a = matrixNew(1, 2, 1)
b = matrixNew(2, 2, 2)
c = matrixConcat(a, b)
plot(matrixRows(c))

1 row + 2 rows → 3 rows.

matrixCopy(matrix)

Copy of the matrix.

An independent (deep) copy of the source; an empty 0×0 matrix if it is not a matrix.

matrix
matrix
The matrix to copy.

Note

The copy is fully separate from the original; changing one does not affect the other. Used to experiment on a matrix without corrupting it.

CODESCRIPT
m = matrixNew(2, 2, 1)
c = matrixCopy(m)
matrixSet(c, 0, 0, 9)
plot(matrixGet(m, 0, 0))

Even after the copy changes, the original [0,0] stays 1.

matrixDet(matrix)

Determinant (square matrix).

The determinant of the matrix (a single number); na if it is not square or not a matrix. Returns 0 for a singular (non-invertible) matrix.

matrix
matrix
The square matrix to take the determinant of.

Note

Computed by elimination with partial pivoting. A determinant of 0 means the matrix has no inverse (its rows/columns are dependent).

Limits

Defined only for a square matrix; returns na otherwise.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 2)
matrixSet(m, 1, 1, 3)
plot(matrixDet(m))

Diagonal 2,3 → determinant 6.

matrixDiff(m1, m2)

Element-wise subtraction (matrix or scalar).

A new matrix with m2 subtracted from m1.

m1
matrix
The matrix to subtract from.
m2
matrix
The matrix to subtract (same size) or a single number to subtract from every cell.

Note

If the second argument is a matrix, subtraction is done cell by cell; if it is a single number, that number is subtracted from all cells. The result is a new matrix.

Limits

When used with two matrices, the sizes must match; a mismatch raises an error.

CODESCRIPT
a = matrixNew(2, 2, 5)
b = matrixNew(2, 2, 2)
d = matrixDiff(a, b)
plot(matrixGet(d, 0, 0))

5-2 = 3 (every cell).

matrixEigenvalues(m)

Array of eigenvalues (symmetric; Jacobi). Result: Arr(len=2, [2.0000000000000004, 0.0])

An array of eigenvalues sorted from largest to smallest; an empty array if it is not square or is empty.

m
matrix
The square (symmetric) matrix to take eigenvalues of.

Note

Computed by the Jacobi method for symmetric matrices. Eigenvalues give the matrix's diagonal magnitudes (its scale along the principal axes).

When to use

To extract principal components (dominant directions) from a covariance matrix — dimensionality reduction, risk concentration.

Limits

The method is for symmetric matrices; on a non-symmetric matrix the result is not meaningful. Verify with matrixIsSymmetric first.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 2)
matrixSet(m, 1, 1, 3)
ev = matrixEigenvalues(m)
plot(arrayGet(ev, 0))

Diagonal 2,3 → largest eigenvalue 3.

matrixEigenvectors(m)

Eigenvector matrix (symmetric; Jacobi).

A new matrix whose columns are the eigenvectors; an empty matrix if it is not square or is empty.

m
matrix
The square (symmetric) matrix to take eigenvectors of.

Note

Computed by the Jacobi method for symmetric matrices; each column of the result matrix is an eigenvector, matching the order of matrixEigenvalues.

Limits

The method is for symmetric matrices; on a non-symmetric matrix the result is not meaningful. Verify with matrixIsSymmetric first.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 2)
matrixSet(m, 1, 1, 3)
v = matrixEigenvectors(m)
plot(matrixRows(v))

2×2 symmetric → eigenvector matrix has 2 rows.

matrixElementsCount(m)

Number of elements (rows×columns).

The total cell count (rows × columns); 0 if it is not a matrix.

m
matrix
The matrix to count.

Note

Useful to check whether a matrix is non-empty before feeding statistics functions (matrixAvg, matrixMedian).

CODESCRIPT
m = matrixNew(2, 3, 0)
plot(matrixElementsCount(m))

2×3 = 6 cells.

matrixFill(matrix, value)

Fills all elements with the value.

Returns nothing (na); it sets every cell of the matrix to the value in place.

matrix
matrix
The matrix to fill.
value
number
The value written to every cell; 0 if non-numeric.

Note

Does not change the size, it just overwrites the contents with a single value. A quick way to reset a matrix before reusing it.

When to use

To reset a whole matrix to a single value (often 0).

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixFill(m, 3)
plot(matrixSum(m))

All four cells become 3 → sum 12.

matrixGet(matrix, i, j)

Gives the cell at a given row and column of the matrix. Rows and columns start at zero.

The cell value at row i, column j; returns na if the index is out of range (no error).

matrix
matrix
The matrix to read from.
i
number
Row index; 0 is the first row.
j
number
Column index; 0 is the first column.

Note

Rows and columns start at 0. An out-of-range index does not crash, it yields na; you can check the result with na().

When to use

To read the value at a specific position. To take a whole row or column, use matrixRow/matrixCol.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 1, 9)
plot(matrixGet(m, 0, 1))

Reads the 9 written at row 0, column 1.

matrixInv(matrix)

Inverse matrix (na if singular).

The inverse of the matrix (a new matrix); na if it is not square or is singular (has no inverse).

matrix
matrix
The square matrix to invert.

Note

Computed by Gauss-Jordan elimination. Multiplying by the inverse yields the identity. A matrix with determinant 0 has no inverse; na is returned then.

Limits

Produces a result only for square, non-singular matrices; otherwise na. Guard the result with na() before using it in a product.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 4)
matrixSet(m, 1, 1, 4)
inv = matrixInv(m)
plot(matrixGet(inv, 0, 0))

Diagonal 4 → inverse diagonal 0.25.

matrixIsAntidiagonal(m)

true if an anti-diagonal matrix. Result: False

true if it is square and every cell outside the anti-diagonal (i+j = n-1) is 0; otherwise false.

m
matrix
The matrix to check.

Note

The anti-diagonal runs from top-right to bottom-left; this check verifies that only that diagonal may be filled.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 1, 3)
matrixSet(m, 1, 0, 4)
plot(matrixIsAntidiagonal(m) ? 1 : 0)

Only the anti-diagonal is filled → 1 (yes).

matrixIsAntisymmetric(m)

true if anti-symmetric. Result: False

true if it is square and each cell equals the negative of its mirror (m[i][j] = -m[j][i]); otherwise false.

m
matrix
The matrix to check.

Note

In an antisymmetric matrix the diagonal is necessarily 0 (since m[i][i] = -m[i][i] holds only for 0).

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 1, 3)
matrixSet(m, 1, 0, -3)
plot(matrixIsAntisymmetric(m) ? 1 : 0)

Diagonal 0, mirrors opposite-signed → 1.

matrixIsBinary(m)

true if all elements are 0/1. Result: True

true if every cell is only 0 or 1, otherwise false.

m
matrix
The matrix to check.

Note

Used to validate structures that should contain only 0-1, such as an adjacency matrix.

CODESCRIPT
m = matrixNew(2, 2, 1)
plot(matrixIsBinary(m) ? 1 : 0)

All cells 1 → 1 (yes).

matrixIsDiagonal(m)

true if a diagonal matrix. Result: False

true if it is square and every off-diagonal cell is 0; otherwise false.

m
matrix
The matrix to check.

Note

The diagonal values may be any number; the condition is only that the off-diagonal is 0.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 5)
matrixSet(m, 1, 1, 7)
plot(matrixIsDiagonal(m) ? 1 : 0)

Only the diagonal is filled → 1 (yes).

matrixIsIdentity(m)

true if an identity matrix. Result: False

true if it is square with a diagonal of 1 and off-diagonal cells of 0; otherwise false.

m
matrix
The matrix to check.

Note

The identity is the neutral element in multiplication (m × identity = m). This check tells whether a matrix is the identity.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 1)
matrixSet(m, 1, 1, 1)
plot(matrixIsIdentity(m) ? 1 : 0)

Diagonal 1, rest 0 → 1 (yes).

matrixIsSquare(m)

true if a square matrix. Result: True

true if the row count equals the column count, otherwise false.

m
matrix
The matrix to check.

Note

Used as a guard before functions that require a square matrix (determinant, inverse, power, trace).

CODESCRIPT
m = matrixNew(3, 3, 0)
plot(matrixIsSquare(m) ? 1 : 0)

3×3 is square → 1 (yes).

matrixIsStochastic(m)

true if stochastic (row sums equal 1). Result: False

true if every row sums to 1 (within a small rounding tolerance); otherwise false.

m
matrix
The matrix to check.

Note

This is a row-stochastic check; being square is not required, only that each row sums to 1. Used to validate transition (Markov) matrices.

CODESCRIPT
m = matrixNew(2, 2, 0.5)
plot(matrixIsStochastic(m) ? 1 : 0)

Each row 0.5+0.5 = 1 → 1 (yes).

matrixIsSymmetric(m)

true if symmetric (m=mᵀ). Result: True

true if it is square and equals its transpose (m[i][j] = m[j][i]); otherwise false.

m
matrix
The matrix to check.

Note

A symmetric matrix is a mirror across the diagonal. Covariance and correlation matrices are symmetric; the eigenvalue functions are designed for such matrices.

CODESCRIPT
m = matrixNew(2, 2, 5)
plot(matrixIsSymmetric(m) ? 1 : 0)

All cells equal → symmetric → 1.

matrixIsTriangular(m)

true if triangular (upper/lower). Result: False

true if it is square and either lower- or upper-triangular (one side of the diagonal is all 0); otherwise false.

m
matrix
The matrix to check.

Note

In an upper-triangular matrix the area below the diagonal is zero, in a lower-triangular one the area above. Either case qualifies as triangular.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 1)
matrixSet(m, 0, 1, 2)
matrixSet(m, 1, 1, 3)
plot(matrixIsTriangular(m) ? 1 : 0)

Below-diagonal is 0 → upper-triangular → 1.

matrixIsZero(m)

true if all elements are zero. Result: False

true if every cell is 0, false if even one is non-zero.

m
matrix
The matrix to check.

Note

Can be used to check whether a computation wrote any value into the matrix.

CODESCRIPT
m = matrixNew(2, 2, 0)
plot(matrixIsZero(m) ? 1 : 0)

All cells 0 → 1 (yes).

matrixKron(m1, m2)

Kronecker product.

The Kronecker product of the two matrices; a new matrix of size (m1.rows×m2.rows) × (m1.cols×m2.cols).

m1
matrix
The left matrix.
m2
matrix
The right matrix.

Note

Each cell of m1 is multiplied by all of m2 and placed block by block. The result grows to the product of both sizes.

CODESCRIPT
a = matrixNew(2, 2, 1)
b = matrixNew(2, 2, 1)
k = matrixKron(a, b)
plot(matrixRows(k))

2×2 ⊗ 2×2 → 4×4, row count 4.

matrixMax(m)

Largest element.

The largest value across all cells; na if the matrix is empty or not a matrix.

m
matrix
The matrix to find the maximum of.

Note

Compares all cells without distinguishing rows or columns.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 1, 1, 8)
plot(matrixMax(m))

One cell is 8 → maximum 8.

matrixMedian(m)

Median of all elements.

The median of all cells; if the cell count is even, the mean of the two middle values. na if empty.

m
matrix
The matrix to take the median of.

Note

All cells are sorted and the middle value is taken. The median is less sensitive to outliers than the mean.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 1)
matrixSet(m, 0, 1, 2)
matrixSet(m, 1, 0, 3)
matrixSet(m, 1, 1, 4)
plot(matrixMedian(m))

1,2,3,4 → mean of the two middle values is 2.5.

matrixMin(m)

Smallest element.

The smallest value across all cells; na if the matrix is empty or not a matrix.

m
matrix
The matrix to find the minimum of.

Note

Compares all cells without distinguishing rows or columns.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, -5)
plot(matrixMin(m))

One cell is -5 → minimum -5.

matrixMode(m)

Most frequent element.

The most frequently repeated cell value; the smallest one if several share the top frequency. na if empty.

m
matrix
The matrix to find the mode of.

Note

When several values share the highest frequency, the smallest value is chosen (a stable, deterministic result).

CODESCRIPT
m = matrixNew(2, 2, 1)
matrixSet(m, 0, 0, 5)
plot(matrixMode(m))

Three 1s and one 5 → mode 1.

matrixMult(a, b)

Matrix multiplication. b can be a matrix (returns a new matrix), a scalar (every cell multiplied by b, returns a new matrix), or a vector/array (a's column count must equal b's length; returns an ARRAY with as many elements as a's row count). a must always be the matrix.

If b is a matrix: the a×b matrix product (a new matrix of size a.rows × b.cols). If b is a number: a new matrix the same size as a, with every cell multiplied by b. If b is an array: a new ARRAY (not a matrix) with as many elements as a has rows.

a
matrix
The left matrix (rows×inner). Must always be the matrix.
b
matrix|number|array
The right-hand value — a matrix (its row count must equal a's column count), a number (scalar — every cell of the matrix is multiplied by it), or an array (its length must equal a's column count — vector product).

Note

Matrix×matrix is true matrix multiplication (not element-wise): result[i][j] is the dot product of a's row i with b's column j. Matrix×array applies the same dot-product logic per row: result[i] is the dot product of a's row i with the array. Matrix×number is plain scalar multiplication.

Limits

a must always be the matrix — the order cannot be flipped even when b is a scalar or array (matrixMult(number, a) is not supported). The inner dimensions must match: for matrix×matrix, a's column count must equal b's row count; for matrix×array, a's column count must equal the array's length — otherwise it raises an error. The order matters (a×b ≠ b×a).

This is true matrix multiplication (not element-wise): result[i][j] is the dot product of a's row i with b's column j.

CODESCRIPT
a = matrixNew(2, 2, 0)
matrixSet(a, 0, 0, 1)
matrixSet(a, 1, 1, 1)
b = matrixNew(2, 2, 5)
p = matrixMult(a, b)
plot(matrixGet(p, 0, 0))

Identity × b = b → [0,0] = 5.

matrixNew(row, column, start?)

Creates a table of rows and columns, filling every cell with the same starting value. Used when you need two-dimensional data; when one dimension is enough an array is simpler.

A new row×column matrix with every cell set to the initial value.

row
number
The row count of the matrix to build; a negative value is clamped to 0.
column
number
The column count; a negative value is clamped to 0.
start
number
Initial value for every cell (optional). If omitted or non-numeric, 0 is used.

Note

A matrix is a two-dimensional grid of numbers made of rows and columns; every other matrix function expects an object built this way.

When to use

To allocate a fixed-size number grid before a computation — a weight table, a transition matrix, a covariance accumulator.

CODESCRIPT
m = matrixNew(2, 3, 0)
plot(matrixElementsCount(m))

A 2×3 matrix with 6 cells.

CODESCRIPT
m = matrixNew(2, 2, 5)
plot(matrixSum(m))

All four cells are 5 → sum 20.

matrixPinv(m)

Moore-Penrose pseudo-inverse.

The pseudo-inverse of the matrix (the Moore-Penrose left inverse); na if (mᵀm) cannot be inverted.

m
matrix
The matrix to pseudo-invert.

Note

Computed as (mᵀm)⁻¹mᵀ; it gives the least-squares solution for non-square or non-invertible matrices. For a square invertible matrix the result equals the ordinary inverse.

When to use

When solving over-determined (rows > columns) linear systems by least squares.

Limits

If the columns are dependent (mᵀm becomes singular) no result is produced and na is returned; guard the result with na().

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 2)
matrixSet(m, 1, 1, 4)
p = matrixPinv(m)
plot(matrixGet(p, 0, 0))

Diagonal 2 → pseudo-inverse diagonal 0.5.

matrixPow(m, n)

Matrix power (square matrix).

The matrix raised to the n-th power (matrix×matrix multiplied n times); a new matrix.

m
matrix
The square matrix to raise to a power.
n
number
The exponent (integer); treated as 0 if negative. 0 yields the identity matrix.

Note

Multiplies the matrix by itself n times (not an ordinary numeric power). At n=0 the identity, at n=1 the matrix itself is returned.

Limits

Defined only for a square matrix; raises an error otherwise.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 0, 0, 2)
matrixSet(m, 1, 1, 3)
p = matrixPow(m, 2)
plot(matrixGet(p, 0, 0))

Diagonal 2 → squared 4.

matrixRank(m)

Matrix rank (via Gaussian elimination).

The rank of the matrix (the number of independent rows/columns); 0 if it is not a matrix.

m
matrix
The matrix to compute the rank of.

Note

Computed by elimination. If the rank is less than the row or column count, the matrix has dependency (redundancy) and no inverse.

CODESCRIPT
m = matrixNew(2, 2, 5)
plot(matrixRank(m))

All rows identical → rank 1.

matrixRemoveCol(m, i?)

Removes column i (returns an array). Result: Arr(len=2, [1.0, 1.0])

An array of the removed column's values; an empty array if the index is out of range.

m
matrix
The matrix to remove a column from.
i
number
The column index to remove.

Note

Deletes the column from the matrix (the column count drops by one) and returns that column as an array. Modifies in place.

CODESCRIPT
m = matrixNew(2, 3, 0)
matrixSet(m, 0, 1, 5)
c = matrixRemoveCol(m, 1)
plot(arrayGet(c, 0))

First value of the removed column 1 is 5.

matrixRemoveRow(m, i?)

Removes row i (returns an array). Result: Arr(len=2, [1.0, 1.0])

An array of the removed row's values; an empty array if the index is out of range.

m
matrix
The matrix to remove a row from.
i
number
The row index to remove.

Note

Deletes the row from the matrix (the row count drops by one) and returns that row as an array. Modifies in place.

CODESCRIPT
m = matrixNew(3, 2, 0)
matrixSet(m, 1, 0, 9)
r = matrixRemoveRow(m, 1)
plot(arrayGet(r, 0))

First value of the removed row 1 is 9.

matrixReshape(m, row?, column?)

Reshapes the matrix in place (returns the same matrix).

A new matrix that places the same cells into the new rows×columns layout.

m
matrix
The matrix to reshape.
row
number
The new row count.
column
number
The new column count.

Note

Cells are read row-major and poured into the new shape. If the new size is larger, missing cells become 0; if smaller, extra cells are dropped. Does not change the source.

CODESCRIPT
m = matrixNew(2, 3, 4)
r = matrixReshape(m, 3, 2)
plot(matrixRows(r))

2×3 (6 cells) → 3×2, row count 3.

matrixReverse(m)

Reverses row and column order.

Returns nothing (na); it reverses the row and column order in place.

m
matrix
The matrix to reverse.

Note

Reverses both the order of rows and the cells within each row; the first cell swaps with the last (a 180° rotation effect). Modifies in place.

CODESCRIPT
m = matrixNew(1, 3, 0)
matrixSet(m, 0, 0, 1)
matrixSet(m, 0, 2, 3)
matrixReverse(m)
plot(matrixGet(m, 0, 0))

[1,0,3] → reversed [3,0,1], first cell 3.

matrixRow(matrix, i)

Returns the ith row as an array. Result: Arr(len=2, [1.0, 1.0])

An array holding a copy of row i; an empty array if the index is out of range.

matrix
matrix
The matrix to take a row from.
i
number
Row index; 0 is the first row.

Note

The returned array is a copy; modifying it does not affect the source matrix. An empty result reads as size 0 with arraySize.

When to use

To process all values of a row with array functions (arraySum, arrayAvg).

CODESCRIPT
m = matrixNew(2, 3, 4)
r = matrixRow(m, 0)
plot(arraySum(r))

Row 0 is [4,4,4] → sum 12.

matrixRows(matrix)

Gives the number of rows in the matrix. Used as a loop bound or to check whether the matrix is empty.

The number of rows in the matrix; 0 if it is not a matrix.

matrix
matrix
The matrix to measure.

Note

Used to know the bound before looping over rows. For the column count use matrixCols.

CODESCRIPT
m = matrixNew(3, 2, 0)
plot(matrixRows(m))

3 rows.

matrixSet(matrix, i, j, value)

Changes the cell at a given row and column of the matrix. It does not create a new matrix but updates the existing one. Rows and columns start at zero.

Returns nothing (na); it modifies the matrix in place.

matrix
matrix
The matrix to modify.
i
number
Row index to write.
j
number
Column index to write.
value
number
The value to store in the cell; if non-numeric, 0 is written.

Note

Overwrites an existing cell; it does not grow the matrix. If the index is out of range it silently does nothing. To add a new row/column use matrixAddRow/matrixAddCol.

When to use

To fill the cells of a matrix allocated with matrixNew one by one.

CODESCRIPT
m = matrixNew(2, 2, 0)
matrixSet(m, 1, 1, 7)
plot(matrixGet(m, 1, 1))

The bottom-right cell is set to 7.

matrixSort(m, column?, ascending?)

Sorts rows by a column (defaults to column 0).

Returns nothing (na); it sorts the matrix rows in place.

m
matrix
The matrix to sort.
column
number
The column index to sort by (optional, defaults to 0).
ascending
bool
true → ascending (small to large), false → descending (optional, default ascending).

Note

Rows are reordered as whole units by the value in the chosen column (each row stays intact internally). Does nothing if the column index is invalid.

When to use

To rank rows by a criterion (e.g. a score column) — bringing the best/worst rows to the front.

CODESCRIPT
m = matrixNew(2, 1, 0)
matrixSet(m, 0, 0, 5)
matrixSet(m, 1, 0, 1)
matrixSort(m, 0, true)
plot(matrixGet(m, 0, 0))

Ascending sort → the smallest (1) comes first.

matrixSubmatrix(m, r1?, r2?, c1?, c2?)

Submatrix (block).

A new matrix made of rows [r1, r2) and columns [c1, c2).

m
matrix
The matrix to slice.
r1
number
The start row (inclusive).
r2
number
The end row (exclusive).
c1
number
The start column (inclusive).
c2
number
The end column (exclusive).

Note

The end indices are exclusive (r2 and c2 are not included). Does not change the source; returns a copy of the sliced region.

When to use

To process a specific block (a window, a sub-table) of a large matrix separately.

CODESCRIPT
m = matrixNew(3, 3, 0)
matrixSet(m, 1, 1, 5)
s = matrixSubmatrix(m, 1, 2, 1, 2)
plot(matrixGet(s, 0, 0))

The single middle cell (5) is taken.

matrixSum(matrix)

Sum of all elements.

The sum of all cells; 0 if it is not a matrix.

matrix
matrix
The matrix to sum.

Note

Sums all values without distinguishing rows or columns. Yields 0 for an empty matrix.

CODESCRIPT
m = matrixNew(2, 2, 2.5)
plot(matrixSum(m))

Four cells of 2.5 → sum 10.

matrixSwapColumns(m, i, j)

Swaps two columns.

Returns nothing (na); it swaps columns i and j in place.

m
matrix
The matrix whose columns are swapped.
i
number
The first column index.
j
number
The second column index.

Note

Swaps two whole columns. Does nothing if either index is out of range.

CODESCRIPT
m = matrixNew(1, 2, 0)
matrixSet(m, 0, 0, 1)
matrixSet(m, 0, 1, 2)
matrixSwapColumns(m, 0, 1)
plot(matrixGet(m, 0, 0))

Columns swapped → [0,0] is now 2.

matrixSwapRows(m, i, j)

Swaps two rows.

Returns nothing (na); it swaps rows i and j in place.

m
matrix
The matrix whose rows are swapped.
i
number
The first row index.
j
number
The second row index.

Note

Swaps two whole rows. Does nothing if either index is out of range.

CODESCRIPT
m = matrixNew(2, 1, 0)
matrixSet(m, 0, 0, 1)
matrixSet(m, 1, 0, 2)
matrixSwapRows(m, 0, 1)
plot(matrixGet(m, 0, 0))

Rows swapped → [0,0] is now 2.

matrixTrace(matrix)

Sum of the diagonal (trace).

The sum of the main-diagonal (i==j) cells; 0 if it is not a matrix.

matrix
matrix
The matrix to take the trace of.

Note

For a non-square matrix it sums the diagonal up to the shorter side. The trace reduces the diagonal energy to a single number.

CODESCRIPT
m = matrixNew(3, 3, 0)
matrixSet(m, 0, 0, 1)
matrixSet(m, 1, 1, 2)
matrixSet(m, 2, 2, 3)
plot(matrixTrace(m))

Diagonal 1+2+3 = 6.

matrixTranspose(matrix)

Transpose (new matrix).

A new matrix with rows and columns swapped (of size cols×rows).

matrix
matrix
The matrix to transpose.

Note

Does not change the source, it returns a new matrix. Cell at row i, column j becomes row j, column i in the output.

When to use

To turn row vectors into column vectors, or to make dimensions fit for a product.

CODESCRIPT
m = matrixNew(2, 3, 0)
t = matrixTranspose(m)
plot(matrixRows(t))

2×3 → transpose is 3×2, row count 3.