Title

matmath -- Matrix operations made simple | 2014-10-24

2014-10-27
matmath a = round(b), rowdecimals(. 1 ...) coldecimals() fmttype(f e g)
. means no round; one for each row or col; can't do both
2015-01-20
matmath a = noomit(b) : remove o. rows and columns
2015-04-11
lm1 for last - 1
2015-04-11
rowdrop() rowkeep coldrop colkeep
but before list or transpose
get rid of drop() keep()
2015-04-19
matmath a, rowdrop....()
matmath a = b, rowdrop....
matmath b, list
matmath a = b, list
2015-04-20
rownumber colnumber

General syntax

Element by element operations on two matrices

matmath newmatrix = leftmatrix operator rightmatrix

Transformations of all elements of a matrix

matmath newmatrix = operator(sourcematrix)

Bookkeeping operations on rows or columns of matrix

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

General information

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.

leftmatrix, rightmatrix, and sourcematrix can
be e() or r() matrices such as e(b), e(V), r(b), r(V), r(table), etc.

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

before Place left matrix before right matrix using mat_capp

above Place matrix above right using mat_rapp

note: Use mat a = b , c or mat a = b \ c
for normal concatenation

Transformations on a matrix

matmath newmatrix = operator(sourcematrix), options

Operator What it does

abs Absolute value

sqrt Square root

sq Square

log Natural log

exp exponential

fill Fill sourcematrix with scale in option value().

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

diag Extract diagonal of sourcematrix.

keep Keep rows and/or columns in rows() and columns().
The ordering of rows and columns in rows() and columns() is used.

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

insert Insert one row or one column filled with
the value specified by value() using the row/column name from name().
The row/colunm number can be from 0 to the current number of rows. The new
row/column is inserted at that location (e.g., row(0) inserts what becomes
row 1).

sort Sort by values of single row or column
specified by row() or column().
You cannot sort rows and columns in same operation.

Options for keep, drop, and sort

rows(numlist)
For keep and drop, select rows from leftmatrix. l indicates the last row.
For sort, indicate the row that will be sorted.

columns(numlist)
For keep and drop, select columns from leftmatrix. l indicates the last column.
For sort, indicate the column that will be sorted.

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.

Author

Scott Long
Home
SPost
Workflow