Matrices
Matrices
Matrices
Matrix M by N
• Matrix – a rectangular array of numbers arranged
in m horizontal rows and n vertical columns,
enclosed in square brackets
• We say A is a m by n matrix, written as m x n
• If m = n, then A is a
3 20 60 50 0
9 0
4 0
2
square matrix of size
n 0 1 0 0 0 0 3
0
• The main diagonal of 0 0 4 0 0 0 4
0
a square matrix A is 06 06 0 1 0 0 7
0
a11 a22 … ann
0 0 0 0 5 0 8
0
• If every entry off the
main diagonal is zero, 0 0 06 02 02 6 8
0
i.e. aik = 0 for i k, 0 0 0 0 0 0 9
then A is a diagonal
matrix m = n = 7 square matrix and diagonal
Special Matrices
• Identity matrix – a 1 0 0 0
diagonal matrix with 1’s 0 1 0 0
on the diagonal; zeros
0 0 1 0
elsewhere
0 0 0 1
A B Result
2 12 13 6
+ =
8 10 8 9
6 4 11 16
Sum Row 1 Col 1
A B Result
2 12 13 6 15
+ =
8 10 8 9
6 4 11 16
2 + 13 = 15
Sum Row 1 Col 2
A B Result
2 12 13 6 15 18
+ =
8 10 8 9
6 4 11 16
12 + 6 = 18
Sum Row 2 Col 1
A B Result
2 12 13 6 15 18
+ = 16
8 10 8 9
6 4 11 16
8 +8 = 16
Sum - Complete
A B Result
2 12 13 6 15 18
+ = 16 19
8 10 8 9
6 4 11 16 17 20
4 + 16 = 20
Product of Two Matrices
• If A is a m x k matrix, then multiplication is
only defined for B which is a k x n matrix
– The result is an m x n matrix
– If A is 5 x 3, then B must be a 3 x k matrix for
any number k >0
– If A is a 56 x 31 and B is a 31 x 10, then the
product AB will by a 56 x 10 matrix
• Let C = AB, then c12 is calculated using the
first row of A and the second column of B
Product Example 1
2 8
4 10 3 5 7
6 12 9 11 13
Product Example 1
A B Result
2 8 3 5 7
4 10 * 9 =
11 13
6 12
Product Row 1 Col 1
A B Result
2 8 3 5 7 78
4 10 * 9 =
11 13
6 12
2 * 3+8* 9 = 78
Product Row 1 Col 2
A B Result
2 8 3 5 7 78 98
4 10 * 9 =
11 13
6 12
2 * 5 + 8 * 11 = 98
Product Row 1 Col 3
A B Result
2 8 3 5 7 78 98 118
4 10 * 9 =
11 13
6 12
2 * 7 + 8 * 13 = 118
Product Row 2 Col 1
A B Result
2 8 3 5 7 78 98 118
4 10 * 9 = 102
11 13
6 12
4 * 3 + 10 * 9 = 102
Product - Complete
A B Product
2 8 3 5 7 78 98 118
4 10 * 9 = 102 130 158
11 13
6 12 126 162 198
6 * 7 + 12 * 13 = 198
Product Example 2
2 8
4 10 3 5 7
6 12 9 11 13
5 3
Product Example 2
A B Product
2 8 3 5 7
4 10 * 9 11 13
=
6 12
5 3
Product Row 1 Col 1
A B Product
2 8 3 5 7 78
4 10 * 9 11 13
=
6 12
5 3
2 * 3 + 8 * 9 = 78
Product Row 1 Col 2
A B Product
2 8 3 5 7 78 98
4 10 * 9 11 13
=
6 12
5 3
2 * 5 + 8 * 11 = 98
Product Row 1 Col 3
A B Product
2 8 3 5 7 78 98 118
4 10 * 9 11 13
=
6 12
5 3
2 * 7 + 8 * 13 = 118
Product Row 2 Col 1
A B Product
2 8 3 5 7 78 98 118
4 10 * 9 11 13
= 102
6 12
5 3
4 * 3 + 10 * 9 = 102
Product - Complete
A B Product
2 8 3 5 7 78 98 118
4 10 * 9 11 13
= 102 130 158
6 12 126 162 198
5 3 42 58 74
5 * 7 + 3 * 13 = 74
Summary of Matrix
Multiplication
• In general, AB BA
– BA may not even be defined
• Properties
– A(BC)=(AB)C
– A(B+C)=AB+AC
– (A+B)C=AC+BC
Boolean (Bit Matrix)
• Each element is
either a 0 or a 1
• Very common in CS
• Easy to manipulate
Join of Bit Matrices (OR)
Meet of Bit Matrices (AND)
Transpose
• The transpose of A, denoted AT, is
obtained by interchanging the rows and
columns of A
• Example
1 3 5 T = 1 2
2 -1 0 3 -1
5 0
Transpose (cont)
• (AT)T=A
• (A+B)T = AT+BT
• (AB)T = BTAT
• If AT=A, then A is symmetric
Inverse
• If A and B are n x n matrices and AB=I, we
say B is the inverse of A
• The inverse of a matrix A, denoted A-1
• It is not possible to define an inverse for
every matrix
Inverse Matrix Example