Matrices and matrix algrebra


Topics in this lab

Introduction

clear all
format short e

Back to the top

Matrices in Matlab

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

Lab exercises

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}$$

Construct the matrix A below.

A = reshape(1:16,4,4);
A =
 
     1     5     9    13
     2     6    10    14
     3     7    11    15
     4     8    12    16
 

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}$


For the second part of the exercise, 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.

  • How many valid matrix-matrix and matrix-vector multiplication operations can you find?
  • What operation can you find that will sum the rows of A? The rows of B? The colums of B?
  • What operation will return the second column of A? The second row of B?
  • What operation will result in exactly the scalar value 1?
  • What operation will result in a 3x3 matrix of 0s and 1s?
  • What operation will result in exactly the scalar value 65?


Can you build A using its lower triangular portions, its upper triangular portion, its diagonal? To get more details on the elementary matrix operators tril, triu and diag, use the Matlab help system.

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)

Powered by MathJax

Published with MATLAB® 8.2