MathMath

Help › Functions

Vectors and matrices

augment stack cols rows length last diag identity matrix submatrix reverse sort csort rsort tr resize

augment(A, B, …) As in Mathcad

Joins matrices or vectors side by side into one wider matrix.

Takes any number of arguments, which must all have the same number of rows. Use stack to join them top to bottom.

Refused

  • Arguments with different numbers of rows.
A≔(13​24​)
B≔(56​)
augment(A,B)=(13​24​56​)

stack(A, B, …) As in Mathcad

Joins matrices or vectors one above the other into one taller matrix.

Takes any number of arguments, which must all have the same number of columns. Use augment to join them side by side.

Refused

  • Arguments with different numbers of columns.
A≔(13​24​)
B≔(5​6​)
stack(A,B)=⎝⎛​135​246​⎠⎞​

cols(A) As in Mathcad

The number of columns in a matrix.

A vector written as a column has one column. See rows for the other way.

A≔(14​25​36​)
cols(A)=3

rows(A) As in Mathcad

The number of rows in a matrix.

A column vector's rows are its elements. See cols for the other way.

A≔(14​25​36​)
rows(A)=2

length(v) As in Mathcad

The number of elements in a vector, which is its number of rows.

For the size of a matrix use rows and cols.

Refused

  • A matrix or a row vector: v must be a single column, as in Mathcad.
v≔⎝⎜⎜⎛​78910​⎠⎟⎟⎞​
length(v)=4

last(v) As in Mathcad

The index of the last element of a vector.

It counts from ORIGIN, so it is the length minus 1 when ORIGIN is 0. It is handy as the top of a range: i := 0..last(v).

Refused

  • A matrix or a row vector: v must be a single column, as in Mathcad.
v≔⎝⎜⎜⎛​78910​⎠⎟⎟⎞​
last(v)=3
vlast(v)​=10

diag(v) As in Mathcad

A square matrix with the vector's elements on its diagonal and zeros elsewhere.

Refused

  • An argument that is not a vector.
diag⎝⎛​⎝⎛​123​⎠⎞​⎠⎞​=⎝⎛​100​020​003​⎠⎞​

identity(n) As in Mathcad

The n by n identity matrix: ones on the diagonal, zeros elsewhere.

identity(3)=⎝⎛​100​010​001​⎠⎞​

matrix(m, n, f) As in Mathcad

An m by n matrix whose element in row i and column j is f(i, j).

f is a function of two arguments, defined elsewhere in the worksheet. The row and column numbers it gets count from ORIGIN, so with ORIGIN = 0 the first element is f(0, 0).

Refused

  • A third argument that is not a function.
f(i,j)≔10⋅i+j
matrix(3,2,f)=⎝⎛​01020​11121​⎠⎞​

submatrix(A, ir, jr, ic, jc) As in Mathcad

The block of A from row ir to row jr and from column ic to column jc, both ends included.

The row and column numbers count from ORIGIN. For a vector, use column 0 for both column arguments (with ORIGIN = 0).

Refused

  • A block that reaches outside the matrix.
A≔⎝⎛​147​258​369​⎠⎞​
submatrix(A,1,2,0,1)=(47​58​)

reverse(A) As in Mathcad

The rows of a vector or matrix in the opposite order.

Applied to a vector it reverses the elements. Applied to a matrix it flips the matrix top to bottom, keeping each row as it is.

reverse⎝⎛​⎝⎛​123​⎠⎞​⎠⎞​=⎝⎛​321​⎠⎞​

sort(v) As in Mathcad

The elements of a vector in ascending order.

For descending order, apply reverse to the result. To sort a matrix by a column or row, see csort and rsort.

Refused

  • Complex elements.
sort⎝⎛​⎝⎛​312​⎠⎞​⎠⎞​=⎝⎛​123​⎠⎞​
reverse⎝⎛​sort⎝⎛​⎝⎛​312​⎠⎞​⎠⎞​⎠⎞​=⎝⎛​321​⎠⎞​

csort(A, c) As in Mathcad

The rows of A, rearranged so that column c is in ascending order.

Each row moves as a whole, so the rows stay intact. The column number c counts from ORIGIN. Rows with equal values in column c keep their order.

Refused

  • A column number outside the matrix.
A≔⎝⎛​312​102030​⎠⎞​
csort(A,0)=⎝⎛​123​203010​⎠⎞​

rsort(A, r) As in Mathcad

The columns of A, rearranged so that row r is in ascending order.

Each column moves as a whole. The row number r counts from ORIGIN. It is csort turned on its side.

Refused

  • A row number outside the matrix.
A≔(310​120​230​)
rsort(A,0)=(120​230​310​)

tr(A) As in Mathcad

The trace of a matrix: the sum of the elements on its main diagonal.

Units

The result has the matrix's units.

A≔(13​24​)
tr(A)=5

resize(M, rows, cols) As in Mathcad

The matrix cut or padded with zeros to the given number of rows and columns, with no scaling.

What fits is kept in place and anything new is 0. To scale an image to a new size, blending its pixels, use resizeImage.

M≔(13​24​)
resize(M,3,3)=⎝⎛​130​240​000​⎠⎞​
resize(M,1,2)=(1​2​)