# 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.

Multiplication

The multiplication of two matrices is performed in a special manner. For a matrix and a matrix , we define the product of and to be the matrix , where

So,

The product of the matrices en can only be formed if the sizes of and are compatible: the number of columns of has to be equal to the number of rows of . If exists, does not need to exist. If as well as exists, we generally do not have that . Within MATLAB, the matrix product of and is always defined if either or 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

>> A+B

and subtraction:

>> A-B

of two matrices and 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 to the power. If is a positive integer, is calculated by repeated multiplication of by itself. The matrix 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.

Transposition

Let be an matrix. Then the transpose of , denoted by , is the matrix with . So:

 (4.2)

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

>> [1 2 3]'

forms the column matrix:
>> 1
2
3


If the matrix contains complex, non-real, elements, then the command A' does not only reflect the matrix with respect to the diagonal, but it also takes the complex conjugate of every element. If you only want to calculate the reflection of 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 such that for given matrices and , where and 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 if is invertible, i.e., it calculates a matrix such that .

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 . has to be a square matrix.

The command:

>> rank(A)

gives the rank of the matrix .

The command:

>> eig(A)

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

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

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

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.

Esteur 2010-03-22