Matrices
INTRODUCTION TO MATLAB
ECE120L
Matrix
A matrix is any rectangular array of numbers or functions:
𝑎11 𝑎12 ⋯ 𝑎1𝑛
𝑎21 𝑎22 ⋯ 𝑎2𝑛
⋮ ⋮ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛
Characteristics of a Matrix
✓ The numbers or functions in the array are called entries or
elements.
✓ If a matrix has m rows and n columns, we say that its size is 𝑚 by
𝑛 (written as 𝑚 × 𝑛).
✓ A matrix with 𝑚 = 𝑛 (or size is 𝑛 × 𝑛) is called a square matrix of
𝑛𝑡ℎ order.
✓ The notation 𝑎𝑖𝑗 represents an element in matrix in the 𝑖𝑡ℎ row
and 𝑗𝑡ℎ column.
Entering Matrices
Given the matrix
16 3 2 13
𝐴 = 5 10 11 8
9 6 7 12
4 15 14 1
To enter this matrix, simply type in the Command Window
>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Row and Column Matrices
An 𝑚 × 1 matrix,
𝑎11
𝑎21
⋮
𝑎𝑚1
is called a column vector. Whereas a 1 × 𝑛 matrix,
𝑎11 𝑎12 ⋯ 𝑎1𝑛
is called a row vector.
Equality of Matrices
Two 𝑚 × 𝑛 matrices A and B are equal if 𝑎𝑖𝑗 = 𝑏𝑖𝑗 for each 𝑖 and 𝑗.
Ex.
1 1 1 1 1
1. The matrices 𝐴 = and 𝐵 = are not equal, since
1 1 1 1 1
the size of the first matrix is 2 × 2 and the size of the second matrix
is 2 × 3.
1 2 1 2
2. The matrices 𝐴 = and 𝐵 = are not equal, since the
3 4 4 3
corresponding entries in the second rows of the matrices are not the
same.
1 2 1 2
3. The matrices 𝐴 = and 𝐵 = are equal.
3 4 3 4
ELEMENT-WISE OPERATIONS
Element-Wise Operations
The matrices involved in element-wise operations should be of the
same sizes. In these operations, the elements on the same row and
column are directly evaluated.
Operation Syntax
Addition .+
Multiplication .x
Division ./
Exponentiation .^
Element-wise Addition
1 3 4 0 2 −2 1 5 2
6 7 8 .+ 1 4 2 = 7 11 10
2 3 −1 1 1 −1 3 4 −2
Element-wise
Multiplication
1 3 4 0 2 −2 0 6 −8
6 7 8 .× 1 4 2 = 6 28 16
2 3 −1 1 1 −1 2 3 1
Element-wise Division
1 3 4 2 2 −2 0.5 1.5 −2
6 7 8 ./ 1 2 2 = 6 3.5 4
2 3 −1 1 1 −1 2 3 1
Element-wise
Exponentiation
.^2
1 3 4 1 3 4 1 3 4 1 9 16
6 7 8 = 6 7 8 .× 6 7 8 = 36 49 64
2 3 −1 2 3 −1 2 3 −1 4 9 1
MATRIX OPERATIONS
Matrix Addition
If matrices A and B are both of 𝑚 × 𝑛 sizes, then their sum is
𝐴 + 𝐵 = 𝑎𝑖𝑗 + 𝑏𝑖𝑗 Syntax: A+B
𝑚×𝑛
Where: 𝑖 ≤ 𝑚, 𝑗 ≤ 𝑛
𝑎𝑖𝑗 = element of matrix A in the 𝑖𝑡ℎ row and 𝑗𝑡ℎ column
𝑏𝑖𝑗 = element of matrix B in the 𝑖𝑡ℎ row and 𝑗𝑡ℎ column
Note: Two matrices can only be added if and only if
they have the same sizes.
Examples
Add the following matrices:
−4 2 3 −1 0 4 −1 + (−4) 2+0 3+4
1. + =
2 −5 7 6 5 1 2+6 −5 + 5 7 + 1
−5 2 7
=
8 0 8
1 −2 2 3 −4
2. 4 5 + 1 5 6 = can′t be added.
0 1 7 3 2
Scalar Multiple of a
Matrix
If k is a real number, then the scalar multiple of a matrix A is
𝑘𝑎11 𝑘𝑎12 ⋯ 𝑘𝑎1𝑛
𝑘𝑎21 𝑘𝑎22 ⋯ 𝑘𝑎2𝑛
𝑘𝐴 = 𝑘 𝑎𝑖𝑗 = 𝑘𝑎𝑖𝑗 =
𝑚×𝑛 𝑚×𝑛 ⋮ ⋮ ⋮
𝑘𝑎𝑚1 𝑘𝑎𝑚2 ⋯ 𝑘𝑎𝑚𝑛
Ex.
−1 0 −2 0
𝐴=2 = Syntax: k*A
2 3 4 6
Properties of Matrix Addition
and Scalar Multiplication
Matrix Multiplication
Let A be a matrix having m rows and p columns, and let B be a
matrix having p rows and n columns. The product AB is the m x n
matrix 𝑎11 𝑎12 ⋯ 𝑎1𝑝 𝑏11 𝑏12 ⋯ 𝑏1𝑛
𝑎 𝑎22 ⋯𝑎2𝑝 𝑏21 𝑏22 ⋯ 𝑏2𝑛
𝐴𝐵 = 21
⋮ ⋮ ⋮ ⋮ ⋮ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑝 𝑏𝑝1 𝑏𝑝2 ⋯ 𝑏𝑝𝑛
𝑎11 𝑏11 + 𝑎12 𝑏21 + ⋯ + 𝑎1𝑝 𝑏𝑝1 … 𝑎11 𝑏1𝑛 + 𝑎12 𝑏2𝑛 + ⋯ + 𝑎1𝑝 𝑏𝑝𝑛
𝑎21 𝑏11 + 𝑎22 𝑏21 + ⋯ + 𝑎2𝑝 𝑏𝑝1 ⋯ 𝑎21 𝑏1𝑛 + 𝑎22 𝑏2𝑛 + ⋯ + 𝑎2𝑝 𝑏𝑝𝑛
𝐴𝐵 =
⋮
𝑎𝑚1 𝑏11 + 𝑎𝑚2 𝑏21 + ⋯ + 𝑎𝑚𝑝 𝑏𝑝1 ⋯ 𝑎𝑚1 𝑏1𝑛 + 𝑎𝑚2 𝑏2𝑛 + ⋯ + 𝑎𝑚𝑝 𝑏𝑝𝑛
Size of the Product in the Matrix Multiplication
𝐴𝑚×𝑝 𝐵𝑝×𝑛 = 𝐶𝑚×𝑛
Syntax: A*B
should be EQUAL!
Examples
Given matrices A and B, find AB:
5 8 5 8
−4 −3 1 0 1 4
1. 𝐴 = 1 0 , 𝐵 = 2. 𝐴 = 1 0 , 𝐵 =
2 0 2 −1 0 2
2 7 2 7
−2 1 3 −1 2 5 0
−1 0 2
3. 𝐴 = , 𝐵= 1 4. 𝐴 = −1 2 2 ,𝐵 = 1 4 −1
−4 1 −3
−2 4 0 1 0 2 −4
Properties of Matrix
Multiplication
Suppose A, B and C are matrices, such that they can be multiplied or
added together, then
(𝑖) 𝐴𝐵 ≠ 𝐵𝐴 Matrix Multiplication is not Commutative
(𝑖𝑖) 𝐴𝐵 𝐶 = 𝐴(𝐵𝐶) Matrix Multiplication is Associative
(𝑖𝑖𝑖) 𝐴 𝐵 + 𝐶 = 𝐴𝐵 + 𝐴𝐶
𝐵 + 𝐶 𝐴 = 𝐵𝐴 + 𝐶𝐴 Matrix Multiplication is Distributive
Transpose of A Matrix
The transpose of an 𝑚 × 𝑛 matrix is the 𝑛 × 𝑚 matrix denoted by is
denoted by 𝐴𝑇 is equal to
⋯ 𝑎1𝑛 𝑇
𝑎11 𝑎12
𝑇 𝑎21 𝑎22 ⋯ 𝑎2𝑛
𝐴 =
⋮ ⋮ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛
Syntax: A’
𝑎11 𝑎21 ⋯ 𝑎𝑚1
𝑇 𝑎12 𝑎22 ⋯ 𝑎𝑚2
𝐴 =
⋮ ⋮ ⋮
𝑎1𝑛 𝑎2𝑛 ⋯ 𝑎𝑚𝑛
Examples
5 8
𝑇 5 1 2
1. 𝐴 = 1 0 , 𝐴 =
8 0 7
2 7
1 2
1 0 1 4
2. 𝐴 = , 𝐴𝑇 = 0 −1
2 −1 0 2 1 0
4 2
1 3 −1 1 −1 4
3. 𝐴 = −1 2 2 , 𝐴𝑇 = 3 2 0
4 0 1 −1 2 1
Properties of Transpose
Inverse of a Matrix
Let A be an 𝑛 × 𝑛 matrix. If there exists an 𝑛 × 𝑛 matrix A-1 such
that
A x A-1 = A-1 x A = I
𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑏11 𝑏12 ⋯ 𝑏1𝑛 1 0 ⋯ 0
𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑏21 𝑏22 ⋯ 𝑏2𝑛 0 1 ⋯ 0
=
⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛 𝑏 𝑏𝑛2 ⋯ 𝑏𝑛𝑛 0 0 ⋯ 1
𝑛1
The matrix A-1 is said to be the inverse of A.
Syntax: inv(A)
Examples
1 0 3
1. Find the inverse of 𝐴 = 2 −1 −1 .
−4 0 5
2 0 1
2. Find the inverse of 𝐷 = −2 3 4 .
−5 5 6
Matrix Division
Let A and B be an 𝑛 × 𝑛 matrix. Then,
𝑨
= 𝑨𝑩−𝟏
𝑩
Syntax: A*inv(B)
Determinant of a Matrix
Suppose A is an 𝑛 × 𝑛 matrix. Associated with A is a number called the
determinant of A and is denoted by det A. Symbolically, we
distinguish a matrix A from the determinant of A by replacing the
brackets by vertical bars:
𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑎11 𝑎12 ⋯ 𝑎1𝑛
𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑎21 𝑎22 ⋯ 𝑎2𝑛
𝐴= det 𝐴 =
⋮ ⋮ ⋮ ⋮ ⋮ ⋮
𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛 𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛
Syntax: det(A)
Examples
Evaluate the determinant of the following matrices:
5 1 2 4
2 4 7
1. 𝐴 = 6 0 3 2. 𝐵 = −1 0 2 3
1 1 6 1
1 5 3
1 0 0 −4
2 1 −2 1
3. 𝐶 = 0 5 0 4
1 6 1 0
5 −1 1 1
Left Division
Left Division is used to solve linear systems of equations.
Example:
A=[2 6 1; 1 2 -1; 5 7 -4]
B=[7;-1;9]
V=A\B
Special Matrices
• Identity Matrix
• Ones Matrix
• Zero Matrix
• Magic Square
• Uniformly Distributed Pseudorandom Numbers
• Normally Distributed Pseudorandom Numbers
• Diagonal Matrix
Identity Matrix
A diagonal matrix in which the main diagonal elements (𝑎𝑖𝑖 ) are all
equal to 1.
1 0 0
0 1 0
0 0 1
Identity Matrix
SYNTAX DESCRIPTION
X = eye(n) Returns the n-by-n identity matrix.
X = eye(m,n) or Returns an m-by-n matrix with 1s on the diagonal
X = eye([m,n]) and 0s elsewhere. The size inputs m and n should
be nonnegative integers. Negative integers are
treated as 0.
X = eye(size(A)) Returns an identity matrix the same size as A.
Ones Matrix
Matrix in which all elements are equal to one.
1 1 1
1 1 1
1 1 1
Ones Matrix
SYNTAX DESCRIPTION
X = ones(n) Returns the n-by-n matrix of 1s. An error message
appears if n is not a scalar.
X = ones(m,n) or Returns an m-by-n matrix of 1s
X = ones([m,n])
X = ones(m,n,p…) or Returns an m-by-n-by-p-by-… array of 1s. The size
X = ones([m,n,p…]) inputs m,n,p,… should be nonnegative integers.
Negative integers are treated as 0s.
X =ones(size(A)) Returns a matrix of 1s that is the same size as A.
Zero Matrix
Matrix in which all elements are equal to zeros.
0 0 0
0 0 0
0 0 0
Zero Matrix
SYNTAX DESCRIPTION
X = zeros(n) Returns the n-by-n matrix of zeros. An error message
appears if n is not a scalar.
X = zeros(m,n) or Returns an m-by-n matrix of zeros
X = zeros([m,n])
X = zeros(m,n,p…) or Returns an m-by-n-by-p-by-… array of zeros. The size
X = zeros([m,n,p…]) inputs m,n,p,… should be nonnegative integers.
Negative integers are treated as 0s.
X = zeros(size(A)) Returns a matrix of zeros that is the same size as A.
Magic Square
An n-by-n matrix constructed from integers 1 through 𝑛2 with equal
row and column sums. The order n must be a scalar greater than or
equal to 3. The syntax is
𝑀 = 𝑚𝑎𝑔𝑖𝑐(𝑛)
Uniformly Distributed
Pseudorandom Numbers
SYNTAX DESCRIPTION
X = rand(n) Returns the n-by-n matrix containing pseudorandom
values drawn from the standard uniform distribution on
the open interval (0,1).
X = rand(m,n) or Returns an m-by-n matrix containing pseudorandom
X = rand([m,n]) values
X = rand(m,n,p…) or Returns an m-by-n-by-p-by-… array containing
X = rand([m,n,p…]) pseudorandom values
X = rand Returns a scalar
X = rand(size(A)) Returns a matrix containing pseudorandom values that
is the same size as A.
Normally Distributed
Pseudorandom Numbers
SYNTAX DESCRIPTION
X = randn(n) Returns the n-by-n matrix containing pseudorandom
values drawn from the standard normal distribution on
the open interval (0,1).
X = randn(m,n) or Returns an m-by-n matrix containing normal
X = randn([m,n]) pseudorandom values
X = randn(m,n,p…) or Returns an m-by-n-by-p-by-… array containing normal
X = randn([m,n,p…]) pseudorandom values
X = randn(n) Returns a normal scalar
X = randn(size(A)) Returns a matrix containing normal pseudorandom
values that is the same size as A.
Diagonal Matrix
A matrix having nonzero elements only in the diagonal running
from the upper left to the lower right. The syntax
c = diag([1 3 2 8])
Creates a diagonal matrix with elements in the diagonal 1 3 2 and 8.
END