Title

matmath -- Matrix operations made simple | 2014-08-01

General syntax

Operations on two matrices

matmath newmatrix = leftmatrix operator rightmatrix

Transformations of elements of a matrix

matmath newmatrix = operator(sourcematrix)

Bookkeeping operations

matmath newmatrix = task(sourcematrix),
rows(numlist) | columns(numlist) [ value(number) name(string) ]

Retrieving a matrix

matmath newmatrix = task(sourcematrix)

General options

list
List matrix after changes.

transpose
Transpose the matrix after the operation is completed.

format(fmt)
Use fmt to format listing; default is %9.3f.

width(integer)
Width used in format; default is 9.

decimals(integer)
Decimals used in format; default is 3.

Matrices and scalars used

newmatrix is the resulting matrix, which can be an existing matrix. An existing matrix will be over-written. The row and column names in newmatrix correspond to those in leftmatrix.

leftmatrix is the first matrix in binary operations like addition.

rightmatrix is the second matrix in binary operations and must be the same dimension as leftmatrix. Alterntively, it can be a scalar.

sourcematrix is the matrix that is modified.

Overview

matmath makes basic matrix operations simpler than matrix or mata. Binary operations are elment by element operations.

Expanded syntax

Operations on two matrices

matmath newmatrix = leftmatrix operator rightmatrix

Operator What it does

+ Addition of left and right

- Subtraction left from right

* Multiplication left times right

/ Division left by right

^ Exponentiation of left to right power

%of Left as percent of right

%dif Change from left to right as percent of left

, Column concantenate left with right

,, Column concantenate left with right using mat_capp

\ Row concantenate left with right (i.e., stack)

\\ Row concantenate left with right using mat_rapp

Transformations on a matrix

matmath newmatrix = operator(sourcematrix)

Operator What it does

abs Absolute value

sqrt Square root

sq Square

log Natural log

exp exponential

inv Inverse

log10 Log base 10

odds Odds transformation (x/(1-x))

logit Logit transformation log(x/(1-x))

Changing the size and ordering of a matrix

matmath newmatrix = task(sourcematrix), options

Task What it does

keep Keep rows and columns in rows() and columns().

drop Drop rows and columns in rows() and columns(). The original ordering is maintained.

add Add a row and/or a column filled with the value specified by value().

diag Make newmatrix a vector from the diagonal of sourcematrix.

Options for keep and drop

rows(numlist)
Select rows from leftmatrix. l indicates the last row.

columns(numlist)
Select columns from leftmatrix. l indicates the last column.

With keep, the specified order is retained in newmatrix and the same rows can be repeated.

Options for add

row(integer)
A row is added after the specified row. 0 indicates a new row at the top; l indicates a new row at the bottom.

column(integer)
A column is added after the specified column. 0 indicates a new column at the left; l indicates the last column.

value(real or missing)
The new row or column contains this value; by default . is used.

name(string)
Matrix rowname or colname for the added row or colunns.

Retrieving matrices

matmath newmatrix = task(sourcematrix), options

Task What it does

get Make newmatrix equal to sourcematrix.

e Make newmatrix equal to e(sourcematrix), such as matmath a = e(b).

r Make newmatrix equal to r((sourcematrix).

matmath newmatrix = task, options

Task What it does

table Make newmatrix equal to r(table).

eb Make newmatrix equal to e(b).

rb Make newmatrix equal to r(b).

ev Make newmatrix equal to e(V).

rv Make newmatrix equal to r(V).

Author

Scott Long

The SPost website.
The Workflow website.