Matrix operations in MATLAB

With MATLAB it is possible to perform operations and commands on matrices. For this, MATLAB knows a lot of commands. A number of these operations will be/have been treated in the course Linear Algebra. At this moment, the following MATLAB operations are of importance.


The multiplication of two matrices is performed in a special manner. For a $ m \times n$ matrix $ A = a_{ij}$ and a $ n \times p$ matrix $ B=b_{jk}$ , we define the product $ AB$ of $ A$ and $ B$ to be the $ m \times p$ matrix $ C=c_{ik}$ , where

$\displaystyle c_{ik} = \sum_{j=1}^n a_{ij}b_{jk}$    


$\displaystyle \begin{bmatrix}\vdots & \vdots & & \vdots \\ a_{i1} & a_{i2} & \c...
...egin{bmatrix}& \vdots & \\ \cdots & c_{ik} & \cdots \\ & \vdots & \end{bmatrix}$    

The product of the matrices $ A$ en $ B$ can only be formed if the sizes of $ A$ and $ B$ are compatible: the number of columns of $ A$ has to be equal to the number of rows of $ B$ . If $ AB$ exists, $ BA$ does not need to exist. If $ AB$ as well as $ BA$ exists, we generally do not have that $ AB=BA$ . Within MATLAB, the matrix product of $ A$ and $ B$ is always defined if either $ A$ or $ B$ is a number. Multiplication of a matrix by a number (scalar multiplication) boils down to multiplication of all matrix elements by that number. The symbol for matrix multiplication in MATLAB is *, i.e.,

>> A*B

Addition and subtraction


>> A+B

and subtraction:

>> A-B

of two matrices $ A$ and $ B$ is performed by addition and subtraction of the separate elements of the matrix. In MATLAB, these operations are defined if the sizes of the matrices are the same or if one of the matrices is a scalar. In the latter case the scalar is added to or subtracted from every element of the matrix.

Raising to a power

The command

>> A^p

raises the matrix $ A$ to the $ p^{\rm th}$ power. If $ p$ is a positive integer, $ A^p$ is calculated by repeated multiplication of $ A$ by itself. The matrix $ A$ needs to be square (i.e., the number of rows is equal to the number of columns) in order to be able to perform this operation.


Let $ A = a_{ij}$ be an $ m \times n$ matrix. Then the transpose of $ A$ , denoted by $ A^T$ , is the $ n \times m$ matrix $ B = b_{ij}$ with $ b_{ij} = a_{ji}$ . So:

$\displaystyle A^T = \begin{bmatrix}a_{11} & \cdots & a_{1n} \\ \vdots & & \vdot...
... \cdots & a_{m1} \\ \vdots & & \vdots \\ a_{1n} & \cdots & a_{11} \end{bmatrix}$ (4.2)

In MATLAB, the command transpose(A) or A' (the latter only for real valued matrices) calculates the transpose of the matrix $ A$ . To give an example:

>> [1 2 3]'

forms the column matrix:
>> 1

If the matrix $ A$ contains complex, non-real, elements, then the command A' does not only reflect the matrix $ A$ with respect to the diagonal, but it also takes the complex conjugate of every element. If you only want to calculate the reflection of $ A$ with respect to the diagonal, you can do this with the command A.'. We illustrate this by means of two examples.

>> A = [2+3*i 4+5*i
        2     3]

A =
    2.0000+3.0000i 4.0000+5.0000i
    2.0000         3.0000

>> A'

ans =
    2.0000-3.0000i   2.0000
    4.0000-5.0000i   3.0000

>> A.'

ans =
   2.0000+3.0000i   2.0000
   4.0000+5.0000i   3.0000
You need to take this into account when dealing with symbolic matrix elements (or you have to define your symbolic elements as reals).
>> syms a b
>> A = [a b
        1 2]

A =
    a, b
    1, 2

>> A'

ans =
    [ conj(a),       1]
    [ conj(b),       2]

>> A.'

ans =
    [ a, 1]
    [ b, 2]

No division

A division operator is not defined for matrices. However, sometimes one needs to find a matrix $ X$ such that $ AX=B$ for given matrices $ A$ and $ B$ , where $ A$ and $ B$ are of appropriate dimensions. This is discussed in Section 4.4.

Matrix functions in MATLAB

In this paragraph we mention some commands that have a matrix as their argument.

The command:

>> inv(A)

calculates the inverse of a square matrix $ A$ if $ A$ is invertible, i.e., it calculates a matrix $ B$ such that $ AB = BA = I$ .

If no inverse exists, MATLAB will give a warning the matrix is singular. Possibly, it will present a matrix, with Inf as elements, e.g.

>> inv([1 1;0 0])
Warning: Matrix is singular to working precision.

ans =
   Inf   Inf
   Inf   Inf
Sometimes, an inverse with finite elements is found by MATLAB, which may be inaccurate. In that case, MATLAB will present the following warning:
>> inv([1e-8 0;0 1e8])
Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 1.000000e-016.
ans =

  1.0e+008 *
    1.0000         0
         0    0.0000

The command:

>> det(A)

calculates the determinant of $ A$ . $ A$ has to be a square matrix.

The command:

>> rank(A)

gives the rank of the matrix $ A$ .

The command:

>> eig(A)

gives a vector containing the eigenvalues of the matrix $ A$ . The matrix $ A$ has to be square. This command is often used in the form

>> [S,D] = eig(A)

This results in matrices $ S$ and $ D$ , so that $ AS = SD$ . Every column of $ S$ is an eigenvector of $ A$ , while $ D$ is a diagonal matrix with diagonal elements equal to the eigenvalues of $ A$ associated with the eigenvectors in the corresponding columns of $ S$ .

Remark: The commands above can also be used for symbolic matrices. However, you have to be aware of the fact that there are other matrix functions that cannot be used for symbolic matrices. Furthermore, there are also commands that can only be used for symbolic matrices.

Previous      Next      Up      Contents

Esteur 2010-03-22