Matrix Algebra
This section familiarises you with basic matrix algebra operations in MATLAB. Together with the commands from the previous sections, they provide the fundamental building blocks of any MATLAB program.
Standard Matrix operators
The following standard matrix operations from linear algebra are available in MATLAB: Transpose '
, Addition +
, Subtraction -
, Multiplication *
and Exponentiation ^
.
Note that the typical restrictions on matrix dimensions apply. For addition and subtraction, the matrices need to have the same dimensions or one input needs to be a scalar. For matrix multiplication, the inside dimension need to be the same or one input needs to be a scalar. For matrix exponentiation, at least the exponent needs to be a scalar.
Left and right division
Even though matrix division is not defined in linear algebra, left and right division operators (\
and /
) can be used to solve linear equations or to calculate the inverse of a matrix.
Consider the linear matrix equation
where is an matrix, is an matrix and is an matrix.
To solve for , we can use the matrix left division operator \
.
Similarly, the matrix right division operator /
can be used to solve for A in the equation above.
Consider the following example.
Note that there is also an inverse operator inv(A)
. However this operator should not be used to solve a linear equation such as the one above by using X=inv(A)*B
as it is computed in a different way and computationally less efficient.
Matrix inverse
We can use the matrix left division operator \
to efficiently solve for the matrix inverse of an invertible matrix . Note that following the example above, if we set , the left division operator solves for the inverse .
Consider the following example.
As you can see, the inv(A)
operator also solves for the matrix inverse. A^(-1)
is just another way of writing inv(A)
.
Dot operators
MATLAB also provides a set of matrix operators for element-wise operations. Note that to perform element-wise operations, the matrices involved need to have the same dimensions or at least one of them needs to be a scalar.
Basic matrix functions
MATLAB provides a wide array of matrix functions. All functions in MATLAB follows one of the following syntaxes.
where in1, in2, ...
denote the arguments of the function and out1, out2, ...
are the outputs of the functions.
To see the brief summary of any function, enter
into the command window. Alternatively, enter
into the command window to see the detailed documentation and examples.
length
length(X)
returns the maximum dimension of the vector/matrix X which can be either the number of rows or number of columns, depending on which is larger.
size
size(X)
returns the dimensions of the supplied array X
. size(X, dim)
returns the size of dimension dim
.
min, max
min(X), max(X)
compute the minimum or maximum elements of matrix X
column-by-column. By wrapping multiple calls of min or max, we can compute the minimal or maximal element of a matrix.
sum
sum(X)
returns a row vector which contains the column-wise sum of the matrix X.
prod
prod(X)
returns a row vector which contains the column-wise product of the matrix X
. prod(X,dim)
computes the product of the elements of dimension dim
.
mean
mean(X)
returns a row vector which contains the column-wise mean of the matrix X
. mean(X, dim)
returns a vector of means computed over dimension dim
.
var
var(X)
returns a row vector which contains the column-wise variance of the matrix X
. var(X,[],dim)
returns a vector of variances computed over dimension dim
.
std
std(X)
returns a row vector which contains the column-wise standard deviation of the matrix X
. std(X,dim)
returns a vector of standard deviations computed over dimension dim
.
sqrt, exp and log
If applied to a matrix X, these functions are applied element-by-element.
Other functions
Here are some examples of other linear algebra matrix functions which are often used.
chol(X)
- Compute Cholesky-factor of pos. def. matrixdet(X)
- Compute determinant of square matrixtrace(X)
- Compute the trace of square matrixeig(X)
- Compute eigenvalues and -values of square matrixkron(x, y)
- Compute Kronecker product of x and y
Last updated
Was this helpful?