Vector and Matrix Operations
Vector and Matrix Operations
Contents
Theory . . . . . . . . . . . . . . . . . . . . 1
Examples . . . . . . . . . . . . . . . . . . 6
R-software . . . . . . . . . . . . . . . . . . 7
Exercises . . . . . . . . . . . . . . . . . . . 8
Acknowledgment . . . . . . . . . . . . . 9
Solutions Exercises . . . . . . . . . . . . 10
Theory
Vectors and vector arithmetic
In physics, there are a lot of quantities that can not be represented by a single number. For
instance, wind is characterised by both a direction (such as north-west) and a magnitude (say,
50 kilometres per hour). In this respect, one makes use of the concept of vectors to denote both
this direction and magnitude.
A column vector is an ordered collection of numbers that are written in a column, such as
7 0
8.5
10 1 4 0
, , 5.6 , 3 , 0 .
4 2
3.1
1 0
The individual numbers in these columns are called the components of the vector. In the specific
examples above, one can see that the first two vectors have two components, the middle vector
has three components and the final two have four components. In general an n-component
column vector is denoted by
u1
u2
u = . .
..
un
In full analogy, a row vector is an ordered collection of numbers, written in a row. Again here,
the numbers in the vector are referred to as the components and a general n-component row
vector is denoted by v = v1 , v2 , . . . , vn .
1
Data Science Institute, Hasselt University
Definition
Two row vectors, or two column vectors, are said to be equal if and only if the corresponding
components in the vector are equal. Hence, for the vectors
2
a = 5, 2 , b = , c = 5, 2 , d = 2, 5 ,
5
it can be noted that a = c, but that a ̸= b and a ̸= d.
Different operations can be performed using vectors, as discussed in the following sections.
Vector addition
For two n-component column vectors, u and v, their sum u + v is defined by component-wise
addition as follows:
u1 v1 u1 + v1
u2 v2 u2 + v2
u+v = . + . = .
. . ..
. . .
un vn un + vn
Hence, the result is another n-component column vector. In a similar fashion, if u and v are row
vectors, their sum is defined to be
u + v = u1 , u2 , . . . , un + v1 , v2 , . . . , vn
= u1 + v1 , u2 + v2 , . . . , un + vn .
It is important to note that we do not define the addition of vectors unless they are both column
or both row vectors, having the same number of components. Finally, since the order in which
ordinary scalars are added does not affect the result, is is also true that the order in which two
vectors are added does not matter (i.e. u + v = v + u).
Scalar multiplication
The multiplication of a number a with a vector v is defined by the component-wise multiplication
of a with the components of v, i.e.
v1 av1
v2 av2
av = a . = . ,
.. ..
vn avn
Linear combinations
The concepts of vector addition and scalar multiplication can be combined to obtain linear
combinations of vectors. More specifically, consider k vectors, denoted by v1 , . . . , vk . Then,
every new vector of the form
a1 v1 + a2 v2 + . . . + ak vk ,
where the ai are real numbers, is called a linear combination of v1 , . . . , vk .
Vector multiplication
Let u be a row vector and v be a column vector, both with the same number of components n.
We define the product u ∗ v to be
u ∗ v = u1 ∗ v1 + u2 ∗ v2 + . . . + un ∗ vn .
2
Data Science Institute, Hasselt University
Up to this point, we have discussed row and column vectors, together with their most im-
portant operations. In fact, vectors are a special case of a more general concept: matrices. A
matrix is a rectangular array of numbers, written in the following format:
a11 a12 . . . a1n
a21 a22 . . . a2n
A= . .. ,
..
.. . ... .
am1 am2 ... amn
where the entries aij denote real numbers and m and n are integers. As can be seen above,
m represents the number of rows, while n corresponds to the number of columns. Therefore,
we call it an m × n matrix. In the case that m = n, the matrix is called a square matrix.
With this notation, an n-component row vector is also referred to as a 1 × n matrix, while an
n-component column vector is also referred to as a n × 1 matrix.
Definition
Two matrices with the same shape (i.e. the same number of columns and the same number
of rows) are said to be equal if and only if the corresponding entries are equal.
Again here, different operations can be performed using matrices (see following sections).
Matrix addition
Two matrices with the same shape can be added together by adding the corresponding compo-
nents. More specifically,
a11 a12 ... a1n b11 b12 ... b1n
a21 a22 ... a2n b21 b22 ... b2n
A+B = . .. + ..
.. .. ..
.. . ... . . . ... .
am1 am2 . . . amn bm1 bm2 . . . bmn
a11 + b11 a12 + b12 ... a1n + b1n
a21 + b21 a22 + b22 ... a2n + b2n
=
.. .. ..
. . ... .
am1 + bm1 am2 + bm2 . . . amn + bmn
Scalar multiplication
In full similarity to the multiplication of a vector with a scalar, we define the product of a matrix
A with a scalar k by:
a11 a12 . . . a1n ka11 ka12 . . . ka1n
a21 a22 . . . a2n ka21 ka22 . . . ka2n
kA = k . .. = .. .. .
.. ..
.. . ... . . . ... .
am1 am2 . . . amn kam1 kam2 . . . kamn
Matrix multiplication
Let A be an m × k matrix and B be an k × n matrix, then the product matrix C = AB is an
m × n matrix whose components areobtained as follows:
b1j
b2j
cij = ai1 , ai2 , . . . , aik . = ai1 ∗ b1j + ai2 ∗ b2j + . . . + aik ∗ bkj .
..
bkj
It is important to note that, in order to multiply two matrices, the number of columns of the
first matrix has to equal the number of rows of the second matrix.
3
Data Science Institute, Hasselt University
Operations on matrices
Matrix transposition
The transpose of a matrix A is a new matrix, denoted by AT , which is formed by turning all the
rows of A into columns for AT . Alternative notations are At and A′ . In a more formal notation,
we can say that the ith row and jth column element of AT is the jth row and ith column element
of A:
[A]Tij = [A]ji .
Of course, it is clear that in case A is an m × n matrix, then AT is an n × m matrix.
Matrix trace
The trace of a matrix, which is only defined for square matrices (i.e. matrices that have the
same number of rows and columns), is calculated by summing over the diagonal elements of the
matrix. More specifically, for an n × n square matrix A:
n
X
tr(A) = a11 + a22 + . . . + ann = aii .
i=1
Matrix determinant
Similar to the trace, the determinant of a matrix is a special number that can be calculated
for square matrices. The determinant of a matrix A is denoted by det(A) or |A| and can be
calculated as follows:
a11 a12
• For a 2 × 2 matrix A = , the determinant is given by
a21 a22
Note that this corresponds to taking the product of the diagonal elements (a11 a22 ) minus
the product of the remaining elements (a12 a21 ).
a11 a12 a13
• For a 3 × 3 matrix A = a21 a22 a23 , calculations are given by:
a31 a32 a33
|A| = a11 (a22 a33 − a23 a32 ) − a12 (a21 a33 − a23 a31 ) + a13 (a21 a32 − a22 a31 ).
The formula above may look cumbersome, but there is a very simple pattern. More
specifically, we consider the first row of A. The first element in that row (a11 ) is multiplied
with the determinant of the 2 × 2 matrix that is not in the row or column of that first
element, i.e. containing the elements in the indicated box below
a11 a12 a13
a
21 a22 a23
a31 a32 a33
.
4
Data Science Institute, Hasselt University
Similarly, the second element of the first row (a12 ) is multiplied with (-1) and the deter-
minant of the 2 × 2 matrix that is not in the row or column of that second element, i.e.
containing the elements in the indicated box below
a11 a12 a13
a
21 a22 a23
a31 a32 a33
.
Finally, the same applies for the final element in the first row (a13 ), which has to be
multiplied by the determinant of the 2×2 matrix that contains the elements in the indicated
box below
a11 a12 a13
a
21 a22 a23
a31 a32 a33
.
The three obtained products are then added together.
• For higher dimensional matrices, the same pattern applies. More specifically, we consider
the first row of elements. We multiply each of these elements with the determinant of the
matrix that remains after removing all elements in the row and column of the respective
element in the first row. The products that coincide with an element that is in an even
place in the first row receive a minus sign. Finally, all products are added together. Hence:
* plus a11 times the determinant of the matrix that is not in a11 ’s row or column,
* minus a12 times the determinant of the matrix that is not in a12 ’s row or column,
* plus a13 times the determinant of the matrix that is not in a13 ’s row or column,
* minus a14 times the determinant of the matrix that is not in a14 ’s row or column
* ...
Special matrices
One special type of matrices are the so-called symmetric matrices, which are square matrices
that are equal to their transpose. Formally, a matrix A is called symmetric if and only if A = AT .
Another type of matrices that deserves some special attention are the identity matrices, which
are square matrices with ones on the main diagonal and zeros elsewhere. With B being an m × n
matrix, it is a property of matrix multiplication that
Im B = BIn = B,
5
Data Science Institute, Hasselt University
Examples
Vector addition
8.5 1 8.5 + 1 9.5
5.6 + 3 = 5.6 + 3 = 8.6
3.1 5 3.1 + 5 8.1
Matrix transpose, trace and determinant Consider the following square matrix
1 3 5
A = 2 2 4
8 0 6
2 4 2 4 2 2
|A| = 1 ∗ −3∗ +5∗ = 1 ∗ 12 − 3 ∗ (−20) + 5 ∗ (−16) = −8
0 6 8 6 8 0
The reader can verify that tr(A) = tr(AT ) and |A| = |AT |.
6
Data Science Institute, Hasselt University
R-software
R is a free software environment for statistical computing and graphics. It compiles and runs on
a wide variety of UNIX platforms, Windows and MacOS. You can download this software for
free from the website (https://www.r-project.org/). You can also use the basic functions of
R without installing the software using R-Fiddle (http://www.r-fiddle.org/). R-Fiddle is a
tool that provides you with a free and powerful environment to write and run R-code right inside
your browser.
For vector and matrix addition or multiplication with a scalar, the standard operators “+”
and “∗” can be used. Vector and matrix multiplication are indicated by the operator “%∗%”.
Further interesting functions for matrix operations include dim(), t(), tr() (”psych” package)
and det() to get the dimensions, transpose, trace and determinant of a matrix.
7
Data Science Institute, Hasselt University
Exercises
Exercises indicated with a ⋆ are preferable solved using R or R-Fiddle. Exercises indicated with a
⋆ between brackets can be solved using R or R-Fiddle, but a manual calculation is recommended.
a) u+v e) (w + x) ∗ (u − v)
b) 3w + 4x
f) (u − v) ∗ (w + x)
c) u∗x
d) x∗u g) u ∗ w + 3(v ∗ x)
1 0
1 1 −1
A = 0 1 , B=
1 3 −5
2 3
a) AB e) BA
T
b) (AB) f) (BA)T
c) tr(AB) g) tr(BA)
d) det(AB) h) det(BA)
8
Data Science Institute, Hasselt University
Acknowledgment
This contribution was written by S. Abrams, M. Aerts, L. Garcia Barrado, N. Cosemans, S.
Jaspers & Y. Vandendijck.
9
Data Science Institute, Hasselt University
Solutions Exercises
3 2 3+2 5
1. a) −2 + 4 = −2 + 4 = 2
5 1 5+1 6
b) 3 ∗ −7, 0, 3 + 4 ∗ 2, 1, −1 = −21, 0, 9 + 8, 4, −4 = −13, 4, 5
3 3∗2 3∗1 3 ∗ −1 6 3 −3
c) −2 ∗ 2, 1, −1 = −2 ∗ 2 −2 ∗ 1 −2 ∗ −1 = −4 −2 2
5 5∗2 5∗1 5 ∗ −1 10 5 −5
3
d) 2, 1, −1 ∗ −2 = 2 ∗ 3 + 1 ∗ (−2) + (−1) ∗ 5 = 6 − 2 − 5 = −1
5
3 2 1
e) −7, 0, 3 + 2, 1, −1 ∗ −2 − 4 = −5, 1, 2 ∗ −6 = −3
5 1 4
3 2 1 −5 1 2
f) −2 − 4∗ −7, 0, 3 + 2, 1, −1 = −6∗ −5, 1, 2 = 30 −6 −12
5 1 4 −20 4 8
3 2
g) −2 ∗ −7, 0, 3 + 3 ∗ 4 ∗ 2, 1, −1
5 1
−21 0 9 4 2 −2 −9 6 3
= 14 0 −6 + 3 ∗ 8 4 −4 = 38 12 −18
−35 0 15 2 1 −1 −29 3 12
1 0 1 1 −1
1 1 −1
2. a) 0 1 ∗
= 1 3 −5
1 3 −5
2 3 5 11 −17
T
1 1 −1 1 1 5
b) 1 3 −5 = 1 3 11
5 11 −17 −1 −5 −17
1 1 −1
c) tr1 3 −5 = 1 + 3 − 17 = −13
5 11 −17
d)
1 1 −1
3 −5 1 −5 1 3
det 1 3 −5 = 1 ∗ −1∗ + (−1) ∗
11 −17 5 −17 5 11
5 11 −17
= 1 ∗ 4 − 1 ∗ 8 + (−1) ∗ (−4) = 0
10
Data Science Institute, Hasselt University
1 0
1 1 −1 −1 −2
e) ∗ 0 1 =
1 3 −5 −9 −12
2 3
T
−1 −2 −1 −9
f) =
−9 −12 −2 −12
−1 −2
g) tr = −1 − 12 = −13
−9 −12
−1 −2
h) det = (−1) ∗ (−12) − (−9) ∗ (−2) = −6
−9 −12
3. a)
λ λ+1 λ+2
4 6 2 6 2 4
det 2 4 6 =λ∗ − (λ + 1) ∗ + (λ + 2) ∗
3 2 2 2 2 3
2 3 2
= λ ∗ (−10) − (λ + 1) ∗ (−8) + (λ + 2) ∗ (−2)
= −4λ + 4
b) −4λ + 4 = 0 ⇔ λ = 1
1 0 −1 λ λ+1 λ+2 λ−2 λ−2 λ
c) 2 −1 0 ∗ 2 4 6 = 2λ − 2 2λ − 2 2λ − 2
2 0 −1 2 3 2 2λ − 2 2λ − 1 2λ + 2
2
a 0 a 0 a 0 1 0 a − 3a + 2 0
4. ∗ −3∗ +2 =
0 2 0 2 0 2 0 1 0 0
⇒ a2 − 3a + 2 = 0 ⇔ a = {1, 2}
2 3 4
5. A = 3 4 5 ⇒ det(A) = 2 ∗ (−1) − 3 ∗ (−2) + 4 ∗ (−1) = 0
4 5 6
11