You are on page 1of 4

MTH517 Wk 6 MATLAB

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 Addition and Scalar Multiplication


Find a.) A + B, b.) A − B, c.) 2A, d.) 2A − B and e.) B + 21 A.
   
1 −1 2 −1
1. A = ,B= ;
2 −1 −1 8
   
6 −1 1 4
2. A =  2 4 , B = −1 5 ;
−3 5 1 10
   
3 2 −1 0 2 1
3. A = 2 4 5 , B = 5 4 2;
0 1 2 2 1 0
   
2 3 4 0 6 2
4. A = 0 1 −1, B =  4 1 0.
2 0 1 −1 2 4

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

2. Find the inverse of the matrix (if it exists).


 
1 2
(a) ;
3 7
 
1 −2
(b) ;
2 −3
 
1 1 1
(c) 3 5 4;
3 6 5
 
1 2 2
(d)  3 7 9 .
−1 −4 −7

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

Exercises on Matrix Commands


1. Create a 10 × 10 matrix of zeros.
2. Create a 5 × 7 matrix of ones.
3. Randomly generate 2 × 5 matrix of data values between −10 and 10. Find the following:

(a) transpose.
(b) product of each column.
(c) Sum of each column.
(d) matrix size.

You might also like