Matrices can be deﬁned using square brackets. Example 2.2 shows how this is accomplished. It also shows how to deﬁne matrices using other matrices as blocks and how to perform basic arithmetic operations on matrices.
The entries of matrices can be accessed using parentheses after the matrix id value. For example, if A is a matrix currently in memory, the statement:
will take the element of A located in the 3rd row, 4th column and store it in an item with id value b.
When you want to access consecutive entries in a matrix you have to use the range operator, “:". When the range operator is preceded and followed by positive integers, i and j, respectively, with i j, it is taken to mean “from i to j". For example, the statement:
will take the entries of A located in rows 2 to 3, column 4 and store them in a matrix C.
When the range operator is not preceded and not followed by integers, it is taken to mean “all items in the respective dimension". For example, the statement:
will take the entire 4th column of A and store it in a matrix (column vector) D.
Finally, when a single range operator is used to index the elements of a vector or matrix, A, then all entries of A are requested. That is, all three statements:
are equivalent when A is a matrix, and they create a copy of A into a matrix with id value D.
Example 2.3 puts all these together, while Example 2.4 shows how indexing is performed and how the range operator can be used in the left-hand side of assignment statements to alter a block of a matrix already in memory. It also shows how to copy the entries of a matrix A into another matrix B, respecting the dimensions of the left-hand-side matrix.
Matrix addition and subtraction work, as expected, in an element-wise fashion: if and are two matrices and , then is deﬁned such that , . Matrix-matrix multiplication, again as expected, works in a non-element-wise fashion: if is an matrix, is a matrix and , then is an matrix such that:
However, very frequently in practice, one needs to do element-wise multiplication, division or exponentiation of matrices. As it is common in other matrix languages, BayES deﬁnes the following element-wise operators:
- “.*" for element-wise multiplication
- “./" for element-wise division
- “.^" for element-wise exponentiation
Example 2.5 shows how these operators can be used.
|■ Example 2.5|
|▼ Input||▼ Output|
// Define matrices
A = [ 1, 2;
B = [ 5, 4;
// Element-wise multiplication
// Element-wise division
// Element-wise exponentiation
Arithmetic operations contained in an expression are evaluated in the following order:
expressions inside parentheses – (...)
transposition (′), scalar and element-wise exponentiation (^, .^)
unary minus (-)
matrix and element-wise multiplication (*, .*), scalar and element-wise division (/, ./)
addition (+) and subtraction (-)
Operators with equal precedence are evaluated from left to right.
As an example, consider the expression:
B*A′^2 - 0.5^-2*(A+B)
where A and B are matrices:
A and B
The expression above is evaluated in the following way:
BayES provides an array of functions that take matrices as arguments and return matrices. These include:
- special matrix functions like inv(), trace(), diag(), etc.
- functions that provide generalizations of scalar functions like log(), exp(), sqrt(), etc. to matrices and work in an element-wise fashion
- summing, rounding and descriptive-statistics functions like sum(), ﬂoor(), mean(), etc.
- decompositions of matrices like chol() and eig()
Detailed descriptions of these functions are given in Appendix B.