MTH517 WK 6 Lab
MTH517 WK 6 Lab
MTH517 WK 6 Lab
Matrices
Defining Matrices
• A matrix is of size m × n, where m is no. of rows and n is no. of columns. For instance,
3 5 1 4
A = 6 7 4 5
9 8 6 1
of size 3 × 4.
• The matrix above can be written down in MATLAB as
A = [3, 5, 1, 4 ; 6, 7, 4, 5 ; 9, 8, 6, 1].
• Matrices are fundamental to solving system of linear equations and also working in n-dimensional
vector spaces.
Matrix Commands
• prod - Product of each column.
• size (x) - Computes the size of the matrix.
• sort - Sorts each column.
• sum - Sorts each column.
• cross - Computes cross products.
• dot - Computes dot products.
• det - Computes determinant of a square matrix.
• inv - Computes inverse of a square matrix.
• pinv - Computes pseudo-inverse of a matrix.
• rank - Computes rank of a matrix.
• A0 - transpose of matrix A.
1
Special Matrices
• eye(n) - Creates an identity matrix.
• ones(n) - Creates a n × n matrix of ones.
• ones(m, n) - Create a m × n matrix of ones.
• zeros(n) - Creates a n × n matrix of zeros.
• zeros(m, n) - Creates a m × n matrix of zeros.
• rand (n) - Creates a n × n matrix of randomly generated numbers from the interval (0, 1).
– rand (m, n) - Creates a m × n matrix instead.
• randi ([a, b], n) - Creates a n × n matrix of randomly generated integers from a given interval
[a, b] of numbers.
– randi ([a, b], m, n) - Creates a m × n matrix instead.
Matrix Multiplication
Find a.) AB and b.) BA (if they are defined).
1 2 2 −1
1. A = ,B= ;
2 4 −1 8
1 −1 7 1 1 2
2. A = 2 −1 8 , B = 2 1 1;
3 1 −1 1 −3 2
−1 3
1 2
3. A = 4 −5 , B =
;
0 7
0 2
0 −1 0 2
4. A = 4 0 2 ,B= −3 .
8 −1 7 1
1 0 3 −2 4 1 6
5. A = ,B= .
6 13 8 −17 20 4 2
2
Transpose of a Matrix
Find a.) AT , b.) AT A and c.) AAT .
4 2 1
1. A = ;
0 2 −1
1 −1
2. A = 3 4 ;
0 −2
2 1 −3
3. A = 1 4 1 ;
0 2 1
−7 11 12
4. A = 4 −3 1 .
6 −1 3
Inverse of a Matrix
1. Show that B is the inverse of A.
1 2 −2 1
(a) A = ,B= 3 ;
3 4 2
− 21
2 −17 11 1 1 2
(b) A = −1 11 −7, B = 2 4 −3.
0 3 −2 3 6 −5
3
Determinant of a Matrix
Find the determinant of the matrix.
1 4 −2 2 −1 3
a.) 3 2 0 b.) 1 4 4
−1 4 3 1 0 2
2 4 6 −3 0 0
c.) 0 3 1 d.) 7 11 0
0 0 −5 1 2 2
0.1 0.2 0.3 −0.4 0.4 0.3
e.) −0.3 0.2 0.2 f.) 0.2 0.2 0.2
0.5 0.4 0.4 0.3 0.2 0.2
(a) transpose.
(b) product of each column.
(c) Sum of each column.
(d) matrix size.