Matrices
Matrices
Naveen Kumar R
Asst. Professor,
Department of Mathematics
Amrita School of Engineering, Bengaluru
1
Matrices
A matrix is a rectangular array of numbers.
The horizontal lines of numbers form rows and the vertical lines of numbers form columns. A matrix with m
rows and n columns is said to be an m×n matrix.
The entries of an m × n matrix are indexed as follows:
a11 a12 ... a1n 1 a1n
a a22 ... a2 n 1 a2 n
A 21
am1 am 2 amn 1 amn
2
Types of matrices
Column matrix
Row matrix One column and any no: of rows
One row and any no: of columns.
1 a11
1 2 3 1×3 a
2 21
a11 a12 a13 a1n 1n 3 3×1
am1 m1
Square matrix
No: of rows = No: of columns Rectangular matrix
No: of rows≠ No: of columns
1 2 3
1 2 1 2 3
, 5 8 2
3 4 8 4 5
4 0 −1
3
Types of matrices
Diagonal matrix
Elements other than those occurring in the principal
Zero or null matrix diagonal are zero.
All elements are zero 2 0 0
0 4 0
0 0 0 0 0 5
0 0 0
0 0 0 𝑎𝑖𝑗 = 0 for all 𝑖 ≠ 𝑗
𝑎𝑖𝑗 ≠ 0 for all 𝑖 = 𝑗
𝑎𝑖𝑗 = 0 for all 𝑖, 𝑗
4
Types of matrices
Upper triangular matrix
A square matrix whose elements below the main diagonal are all zero
1 2 3
0 5 5
0 0 6
Triangular matrix
A square matrix whose elements above or 𝑎𝑖𝑗 = 0 for all 𝑖 > 𝑗
below the main diagonal are all zero
1 0 0 1 2 3
2 4 0 , 0 5 5 Lower triangular matrix
3 5 6 0 0 6
A square matrix whose elements above the main diagonal are all zero
1 0 0
2 4 0
3 5 6
5
Operations on matrices
Equality of matrices
Two matrices are said to be equal only when all corresponding elements are equal.
Therefore their size or dimensions are equal as well.
1 5 3 1 5 3
𝐴 = 6 −1 10 𝐵 = 6 −1 10
8 9 7 8 9 7
⇒𝐴=𝐵
6
Let’s try…
7
Operations on matrices
Addition of matrices
The sum of two matrices is a matrix obtained by adding the corresponding elements of the
given matrices.
For example,
1 2 3 5 8 0 6 10 3
5 8 2 + 3 10 −2 = 8 18 0
4 0 −1 9 1 1 13 1 0
8
Operations on matrices
Subtraction of matrices
The difference of two matrices is a matrix obtained by subtracting the corresponding elements
of the given matrices.
For example,
1 2 3 5 8 0 −4 −6 3
5 8 2 − 3 10 −2 = 2 −2 4
4 0 −1 9 1 1 −5 −1 −2
9
Properties of matrix addition
𝐴 + 𝐵 = 𝐵 + 𝐴 (Commutative law)
(𝐴 + 𝐵) + 𝐶 = 𝐴 + (𝐵 + 𝐶) (Associative law)
𝐴 + 𝑂 = 𝑂 + 𝐴 = 𝐴 (Existence of additive identity)
O is the additive identity for matrix addition
10
Operations on matrices
Scalar multiplication of matrices
Matrices can be multiplied by a scalar (constant or single element)
Let k be a scalar quantity; then 𝒌𝑨 = 𝑨𝒌.
For example,
1 2 3 2 4 6
2 5 8 2 = 10 16 4
4 0 −1 8 0 −2
𝑘(𝐴 + 𝐵) = 𝑘𝐴 + 𝑘𝐵
𝑘 + 𝑙 𝐴 = 𝑘𝐴 + 𝑙𝐴
11
Operations on matrices
Multiplication of matrices
The product of two matrices A and B is defined if the number of columns of A is equal to the
number of rows of B.
𝑨 × 𝑩 = 𝑨𝑩
12
Matrix multiplication
b11 b12
a11 a12 a13 (a11 b11 ) (a12 b21 ) (a13 b31 ) (a11 b12 ) (a12 b22 ) (a13 b32 )
a b21 b22
21 a23
b31 b32 21 11 32
a22 ( a b ) ( a22 b21 ) ( a23 b31 ) ( a 21 b12 ) ( a 22 b22 ) ( a 23 b )
2× 𝟑 3× 𝟐 2× 𝟐
4 8
1 2 3 (1 4) (2 6) (3 5) (1 8) (2 2) (3 3) 31 21
4 2 7 6 2 (4 4) (2 6) (7 5) (4 8) (2 2) (7 3) 63 57
5 3
13
Properties of matrix multiplication
Assuming that matrices A, B, and C are conformable for the operations indicated, the
following are true:
14
Non-commutativity of multiplication of matrices
For example,
1 2 3 4
A ,B
5 0 0 2
1 2 3 4 3 8
AB
5 0 0 2 15 20
3 4 1 2 23 6
BA
0 2 5 0 10 0
AB BA
15
Zero matrices as the product of two non zero matrices
For real numbers, if 𝑎𝑏 = 0, then either 𝑎 = 0 or 𝑏 = 0. This need not be true for matrices.
For example,
1 1 2 3 0 0
0 0 2 3 0 0
16
Let’s try…
a) b) c)
1 0 0
2) Find the value of 𝐴2025 − 𝐴2020 , if A= 0 1 1
1 0 0
17
Transpose of a matrix
Matrix obtained by interchanging rows and columns is called the transpose of the matrix.
8 5
8 6 0
A A 6 4
T
5 4 2 0 2
18
Properties of transpose of matrices
(𝐴’)’ = 𝐴
(𝐴 + 𝐵)’ = 𝐴’ + 𝐵’
(𝐴𝐵)’ = 𝐵’𝐴’
19
Trace of a matrix
Let A be an n× n matrix. The trace of A, denoted by tr(A) is the sum of the diagonal elements of
A.
a11 a12 ... a1n 1 a1n
a a22 ... a2 n 1
a2 n
A 21
an1 an 2 ann 1 ann
20
Let’s try…
1 2 0
1 2 1 2 3
𝐴= ,𝐵 = 3 8 1 , 𝐶 = .
3 4 4 5 6
−2 7 −5
21
Properties of the Matrix Trace
22
Symmetric matrices
a b T a b
A ,A
b d b d
For example
1 2 3
8 9
9 6 , 2 4 5
3 5 6
23
Skew- symmetric matrices
0 b T 0 b
A ,A
b 0 b 0
For example
0 e f
e 0 g
f g 0
24
Skew-symmetric matrices
Any square matrix can be expressed as the sum of a symmetric and a skew
symmetric matrix.
𝟏 𝟏
A= (A+A’) + (A-A’)
𝟐 𝟐
25
Let’s try…
−1 6
a) A=
2 0
0 8 −3
b) B= 4 1 7
−1 2 5
26
Determinant
Each square matrix A has a unit scalar value called the determinant of A,
denoted by det A or |A|.
a11 a12
A
a21 a22
𝑎11 𝑎12
det 𝐴 = 𝑎 𝑎22 = 𝑎11 𝑎22 − 𝑎21 𝑎12
21
27
Properties of determinants
• The value of the determinant remains unchanged if its rows and columns are interchanged.
• If any two rows(or columns) of a determinant are interchanged, then sign of determinant changes.
• If any two rows(columns) of a determinant are identical, then the determinant is zero.
• If each element of a row (or column) of a determinant is multiplied by a constant k, then its value gets
multiplied by k.
• If some or all elements of a row or column of a determinant are expressed as sum of two(or more) terms,
then the determinants can be expressed as sum of two (or more) determinants.
28
Properties of determinants
Let A and B be 𝑛 × 𝑛 matrix and let 𝑘 be a scalar. Then
• det 𝑘𝐴 = 𝑘 𝑛 det 𝐴
• det 𝐴𝑇 = det 𝐴
• det 𝐴𝐵 = det 𝐴 det 𝐵
• If A is invertible, then
1
• det 𝐴−1 = det(𝐴)
29
Let’s try…
1 5 0 5
7 −10 0 7 0 3 0
𝐴 = 8 −1 4 B
3 6 −5 4 3 5 3
2 8 8 8
30
Minors
If A is an n x n matrix and one row and one column are deleted, the resulting matrix is an (n-1) x (n-1)
submatrix of A.
The determinant of such a submatrix is called a minor of A and is designated by Mij , where i and j
correspond to the deleted row and column, respectively.
a11 a12 a13
Mij is the minor of the element aij in A.
A a21 a22 a23
a31 a32 a33
31
Cofactor
C11 (i 1, j 1) (1)11 M 11 M 11
C12 (i 1, j 2) (1)1 2 M 12 M 12
C13 (i 1, j 3) (1)1 3 M 13 M 13
32
Adjoint of a matrix
𝑎 𝑏 𝑑 −𝑏
If 𝐴 = , 𝑡ℎ𝑒𝑛 𝑎𝑑𝑗(𝐴) =
𝑐 𝑑 −𝑐 𝑎
Change sign Interchange
33
Let’s try…
1 0 1
A 0 2 3
1 0 1
34
Properties of adjoint matrix
35
Inverse of a matrix using adjoint
11
A adjA, A 0
A
Let’s try…
36
What do you observe in the image above? Write this logic so that a computer can do the same for
you .
A flower
Petals
Different colors
Leaves
It will be a challenging task!
….
37 Pic courtesy: https://cdn.britannica.com/45/5645-050-B9EC0205/head-treasure-flower-disk-flowers-inflorescence-ray.jpg
Introduction
You can recognize a flower easily because your brain has evolved
over millions of years to do this automatically, even though we
don’t completely understand how it works. Identifying colors, like
red or black, is something our brains do naturally. But teaching a
computer to do the same thing is hard and is a big area of research
in Machine Learning and Computer Science. Before diving into
how machines identify attributes in an image, let's consider an
essential question: How does a machine store this image?
Today’s computers are designed to process only 0 and 1. So how can an image such as the above with multiple attributes like
color be stored in a computer? This is achieved by storing the pixel intensities in a construct called Matrix. Then, this matrix
can be processed to identify colors, etc.
Thus, any operation on the image will use linear algebra and matrices in the background.
Simple and concise representations of linear equations can be achieved through the use
of matrices.
With the use of linear algebra, one can comprehend and work with matrices to extract
valuable information from data.
39
Matrices for data science: Data representation
Rows represent
samples and
A natural tool columns represent
for arranging data the values of
is the matrix. variables or
On computers, attributes
matrices are
commonly used to
store and Samples
represent data. Rows
(Data points)
Data Matrix
Data matrix
Columns (variables)
40
Where do matrices play role in data science?
A digital grayscale image is presented in the computer by pixels matrix.
Each pixel of such image is presented by one matrix element – an integer from the set {0,1,2,...255}.
Image Matrix
In a matrix, in which all elements are positive or equal to zero, we may present value 0 with black, and maximum
positive value with white color; in this way, all positive matrix values will be presented as grayscale nuances
(from black for zero, to white for maximum positive value).
41
Application in data science
42
Some more applications…
Feature Scaling : Matrices are used to scale features to a specific range or distribution
Principal Component Analysis: Uses matrix operations to reduce the dimensionality of data while preserving as
much variance as possible.
Logistic Regression: Uses matrices to handle multiple features and calculate probabilities for classification.
K-Means Clustering: Matrices are used to compute distances between points and centroids efficiently.
Natural Language Processing: Word embeddings are represented as matrices where each row corresponds to the
vector representation of a word.
Neural Networks: In neural networks, the connections between neurons are represented by weight matrices.
43
44