Back to tutorial index

# Introduction to matrices and matrix algebra

## Introduction

clear all
format short e

Back to the top

## Matrices

A matrix in Matlab is simply a two dimensional array of real numbers. What makes a matrix conceptually different from an array is that we allow certain mathematical operations with matrices that are not always defined for arrays.

Matrices that have the same shape can be added together to form a third matrix with the same shape

A = ones(4,3)
A =

1     1     1
1     1     1
1     1     1
1     1     1


B = 2*ones(4,3)
B =

2     2     2
2     2     2
2     2     2
2     2     2


C = A + B
C =

3     3     3
3     3     3
3     3     3
3     3     3



Matrices can be multiplied together if they are "conformable". Matrix A can be multiplied on the left by matrix B if the number of columns of A is the same as the number of rows of B. For example,

A = ones(3,4)
A =

1     1     1     1
1     1     1     1
1     1     1     1


B = ones(4,2)
B =

1     1
1     1
1     1
1     1


C = A*B
C =

4     4
4     4
4     4



Trying to multiply A on the left by B will result in an error.

C = B*A


Error using *
Inner matrix dimensions must agree.

Error in lab_5 (line 391)
C = B*A


The reason for this is that the number of columns of B is two, but the number of rows in A is 4. The "inner dimensions" refer to these two dimensions, respectively.

Back to the top

## Elementary matrix operations

Create A by reshaping the integer sequence 1:25 into a 5x5 matrix. Notice how the reshaping is done.

A = reshape(1:25,5,5)
A =

1     6    11    16    21
2     7    12    17    22
3     8    13    18    23
4     9    14    19    24
5    10    15    20    25



The matrix transpose turns rows into columns and columns into rows.

A'
ans =

1     2     3     4     5
6     7     8     9    10
11    12    13    14    15
16    17    18    19    20
21    22    23    24    25



When we transpose matrices that are not square, the shape of the matrix changes as well as its entries.

B = reshape(1:12,6,2)
B =

1     7
2     8
3     9
4    10
5    11
6    12



B' is a 2x6 matrix.

B'
ans =

1     2     3     4     5     6
7     8     9    10    11    12



Back to the top

## Some special matrices

The $N\times N$ identity matrix.

N = 5;

The identity matrix

eye(N)
ans =

1     0     0     0     0
0     1     0     0     0
0     0     1     0     0
0     0     0     1     0
0     0     0     0     1



A matrix of zeros

zeros(N,N)
ans =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0



A matrix of ones

ones(N,N)
ans =

1     1     1     1     1
1     1     1     1     1
1     1     1     1     1
1     1     1     1     1
1     1     1     1     1



The lower triangular part of A

tril(A)
ans =

1     0     0     0     0
2     7     0     0     0
3     8    13     0     0
4     9    14    19     0
5    10    15    20    25



The upper triangular part of A

triu(A)
ans =

1     6    11    16    21
0     7    12    17    22
0     0    13    18    23
0     0     0    19    24
0     0     0     0    25



The diagonal part of A

diag(A)
ans =

1
7
13
19
25



Back to the top

## Creating sparse matrices

In many common situations in engineering applications, the matrix itself is too large to explicitly write down every individual entry. Often, the matrix contains mostly zeros. In this case, the matrix is said to be sparse.
The simplest such matrix is the diagonal matrix, which has non-zero entries only on the diagonal of the matrix. A simple example is the identity matrix,

eye(4)
ans =

1     0     0     0
0     1     0     0
0     0     1     0
0     0     0     1



We can construct more general sparse banded matrices with the diag command. This command takes vectors that will be placed in diagonal and off-diagonal entries. For example,

A = diag(3*ones(4,1),-1) + diag(2*ones(5,1),0) + diag(ones(4,1),1)
A =

2     1     0     0     0
3     2     1     0     0
0     3     2     1     0
0     0     3     2     1
0     0     0     3     2



The second argument indicates where to put the diagonal vector, i.e. along the sub-diagonal (indicated by the second argument -1), the main diagonal (indicated by the second argument 0), or the super-diagonal (indicated by the second argument 1).
When the matrix is used in solving a linear system, it is much more efficient to store only the non-zero entries. We can do this using the spdiags command, i.e.

z = ones(5,1);
A_sparse = spdiags([3*z 2*z z],[-1 0 1],N,N)
A_sparse =

(1,1)        2
(2,1)        3
(1,2)        1
(2,2)        2
(3,2)        3
(2,3)        1
(3,3)        2
(4,3)        3
(3,4)        1
(4,4)        2
(5,4)        3
(4,5)        1
(5,5)        2



This command requires that you provide the diagonals and off-diagonals, the corresponding diagonal position, and the size of the resulting matrix. Note that the matrix is shown in sparse format, in which only the non-zero entries are stored. To see what the matrix looks like, we can use the full command,

full(A_sparse)
ans =

2     1     0     0     0
3     2     1     0     0
0     3     2     1     0
0     0     3     2     1
0     0     0     3     2



Back to the top

## Lab exercises

#### Part I : Basic vector and matrix manipulation

For the following exercises, you will use the matrices and vectors given below.

$A = \left[ \begin{array}{ccc} 3 & -1 & 2 \\ -1 & 1 & 7 \\ 2 & 2 & 1 \end{array}\right],\quad B = \left[ \begin{array}{cccc} 2 & 1 & 1 & -1\\ 0 & 2 & 0 & 12\\ -1 & 1 & 1 & 8 \end{array}\right], \quad C = \left[ \begin{array}{cc} 2 & 1 \\ 0 & 2 \\ -1 & 1 \end{array}\right]$

${\bf x} = \left[\begin{array}{ccc}1 \\ 4 \\ -1\end{array}\right], \quad {\bf y} = \left[\begin{array}{ccc}0 \\ -3 \\ 5\end{array}\right]$

Compute the the following expressions by hand and then check your answers with Matlab. To check your answers, use Matlab's matrix and vector multiplication and the dot function. Start by creating matrices the matrices and vectors above.
1. Identify entries $a_{3,2}$, $b_{1,4}$ and $c_{2,1}$
2. ${\bf x}^T {\bf y} = \phantom{xxxxxxx}$ (Is this an inner product or an outer product?)
3. ${\bf x} {\bf y}^T = \phantom{xxxxxxx}$ (Is this an inner product or an outer product?)
4. ${\bf x} \cdot {\bf y} = \phantom{xxxxxxx}$(Is this an inner product or an outer product?)
5. The dot product of row 2 of $A$ with the vector ${\bf x}$
6. The dot product of column 1 of $B$ with the vector ${\bf y}$
7. $A{\bf x} =$
8. ${\bf y}^T B =$
9. $B^T{\bf x} =$
10. Show that the product $A{\bf x}$ can be expressed as a linear combination of the columns of $A$.
11. Show that the product ${\bf y}^T B$ can be expressed as a linear combination of the rows of $B$.
12. Show that $(AB)^T = B^T A^T$
13. What are the dimensions of $C^TB$?
14. What are the dimensions of $C^T{\bf x}$?

#### Part II : Special matrices

Using the matrix $A$ and vector ${\bf x}$ above, construct the following matrices and vectors. Use the commands triu, tril, diag and eye. The matrices L, U, D and I refer to the lower triangular portion of A, the upper triangular portion of A, the diagonal of A, and the identity matrix, respectively.
1. Construct the matrices L, U, D and I.
2. Use the matrices L, U, D to reconstruct A.
3. Show that $AI = IA$ and that $I{\bf x} = {\bf x}$

#### Part III : Matrix properties

1. The symmetric and skew parts of a square matrix A are given by $$A_{sym} = \frac{A + A^T}{2}, \quad A_{skew} = \frac{A - A^T}{2}$$
and show that the following facts are true for A.
1. $A_{sym} + A_{skew} = A$
2. $(A_{sym})^T = A_{sym}$
3. $(A_{skew})^T = -A_{skew}$

#### Part IV : Matrix and vector norms

A matrix norm measures the "size" of a matrix. Compute the following vector and matrix norms (1) by hand, (2) using elementary Matlab functions, and (3) using Use the vectors and matrices from Part I.
1. $\parallel {\bf x} \!\! \parallel_1 \; =$
2. $\parallel {\bf x} \!\! \parallel_2 \; =$
3. $\parallel {\bf x} \!\! \parallel_{\infty} \; =$
4. $\parallel A \!\! \parallel_1 \; =$
5. $\parallel A \!\! \parallel_2 \; =$
6. $\parallel A \!\! \parallel_{\infty} \; =$
7. Normalize (in any norm) the vector ${\bf x}$ so that has length 1 in the norm that you chose. Verify that your normalized vector has length 1.
8. Use the rand function to generate random vectors of length 5. Then, verify by experimentation that the following is always true : $$\parallel {\bf x} \! \! \parallel_{\infty} \; \le \; \parallel \! {\bf x} \! \parallel_1 \; \le \; 5\parallel \! {\bf x} \parallel_{\infty}$$.
Can you explain why this is always true?

#### Part V : More on matrix-vector multiplication

Construct the following matrices and vectors.

$A = \left[\begin{array}{rrrr} 1 & 5 & -8 & 7 \\ -2 & 0 & 6 & 5 \\ 1 & 2 & 2 & 1 \end{array}\right]$,          $B = \left[\begin{array}{rrr} 1 & 1 & 2\\ -5 & 1 & 4 \\ 2 & 1 & 0 \end{array}\right]$,

$u = \left[\begin{array}{r} 0 \\ 1 \\ 0 \end{array}\right]$,          $v = \left[\begin{array}{r} 1 & 1 & 1 \end{array}\right]$

The following questions ask you about how to combine the above matrices and vectors using matrix multiplication and the transpose operator.
1. How many valid matrix-matrix and matrix-vector multiplication operations can you find?
2. What operation can you find that will sum the rows of A? The rows of B? The colums of B?
3. What operation will return the second column of A? The second row of B?
4. What operation will result in exactly the scalar value 1?
5. What operation will result in a 3x3 matrix of 0s and 1s?
6. What operation will result in exactly the scalar value 65?

Back to the top

## Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_6.m)

Published with MATLAB® 8.5