Note
This personal note is written after studying the coursera opening course, Machine Learning by Andrew NG . And images, audios of this note all comes from the opening course.
Matrices and Vectors
Matrices are 2-dimensional arrays:
$$
\begin{bmatrix}
a&b&c\
d&e&f\
g&h&i\
j&k&l\
\end{bmatrix}
$$
The above matrix has four rows and three columns, so it is a $4 \times 3$ matrix.
A vector is a matrix with one column and many rows:
$$
\begin{bmatrix}
w\x\y\z
\end{bmatrix}
$$
So vectors are a subset of matrices. The above vector is a $4 \times 1$ matrix.
Notation and terms :
- $A_{ij}$ refers to the element in the ith row and jth column of matrix A.
- A vector with ‘n’ rows is referred to as an ‘n’-dimensional vector.
- $v_i$ refers to the element in the ith row of the vector.
- In general, all our vectors and matrices will be 1-indexed. Note that for some programming languages, the arrays are 0-indexed.
- Matrices are usually denoted by uppercase names while vectors are lowercase.
- “Scalar” means that an object is a single value, not a vector or matrix.
- $\mathbb{R}$ refers to the set of scalar real numbers.
- $\mathbb{R}^n$ refers to the set of n-dimensional vectors of real numbers.
Run the cell below to get familiar with the commands in Octave/Matlab. Feel free to create matrices and vectors and try out different things.
1 | % The ; denotes we are going back to a new row. |
Addition and Scalar Multiplication
Addition and subtraction are element-wise , so you simply add or subtract each corresponding element:
$$
\begin{bmatrix} a & b \ c & d \ \end{bmatrix} + \begin{bmatrix} w & x \ y & z \ \end{bmatrix} = \begin{bmatrix} a+w & b+x \ c+y & d+z \ \end{bmatrix}
$$
Subtracting Matrices:
$$
\begin{bmatrix} a & b \ c & d \ \end{bmatrix} - \begin{bmatrix} w & x \ y & z \ \end{bmatrix} =\begin{bmatrix} a-w & b-x \ c-y & d-z \ \end{bmatrix}
$$
To add or subtract two matrices, their dimensions must be the same .
In scalar multiplication, we simply multiply every element by the scalar value:
$$ \begin{bmatrix} a & b \\ c & d \\ \end{bmatrix} * x =\begin{bmatrix} a*x & b*x \\ c*x & d*x \\ \end{bmatrix} $$In scalar division, we simply divide every element by the scalar value:
$$
\begin{bmatrix} a & b \ c & d \ \end{bmatrix} / x =\begin{bmatrix} a /x & b/x \ c /x & d /x \ \end{bmatrix}
$$
Experiment below with the Octave/Matlab commands for matrix addition and scalar multiplication. Feel free to try out different commands. Try to write out your answers for each command before running the cell below.
1 | % Initialize matrix A and B |
Matrix-Vector Multiplication
We map the column of the vector onto each row of the matrix, multiplying each element and summing the result.
$$ \begin{bmatrix} a & b \\ c & d \\ e & f \end{bmatrix} *\begin{bmatrix} x \\ y \\ \end{bmatrix} =\begin{bmatrix} a*x + b*y \\ c*x + d*y \\ e*x + f*y\end{bmatrix} $$The result is a vector. The number of columns of the matrix must equal the number of rows of the vector.
An m x n matrix multiplied by an n x 1 vector results in an m x 1 vector .
Below is an example of a matrix-vector multiplication. Make sure you understand how the multiplication works. Feel free to try different matrix-vector multiplications.
1 | % Initialize matrix A |
Matrix-Matrix Multiplication
We multiply two matrices by breaking it into several vector multiplications and concatenating the result.
$$ \begin{bmatrix} a & b \\ c & d \\ e & f \end{bmatrix} *\begin{bmatrix} w & x \\ y & z \\ \end{bmatrix} =\begin{bmatrix} a*w + b*y & a*x + b*z \\ c*w + d*y & c*x + d*z \\ e*w + f*y & e*x + f*z\end{bmatrix} $$An m x n matrix multiplied by an n x o matrix results in an m x o matrix. In the above example, a 3 x 2 matrix times a 2 x 2 matrix resulted in a 3 x 2 matrix.
To multiply two matrices, the number of columns of the first matrix must equal the number of rows of the second matrix.
For example:
1 | % Initialize a 3 by 2 matrix |
Matrix Multiplication Properties
- Matrices are not commutative: $A∗B≠B∗A,A∗B≠B∗A$
- Matrices are associative: $(A∗B)∗C=A∗(B∗C)$
The identity matrix , when multiplied by any matrix of the same dimensions, results in the original matrix. It’s just like multiplying numbers by 1. The identity matrix simply has 1’s on the diagonal (upper left to lower right diagonal) and 0’s elsewhere.
$$
\begin{bmatrix} 1 & 0 & 0 \ 0 & 1 & 0 \ 0 & 0 & 1 \ \end{bmatrix}
$$
When multiplying the identity matrix after some matrix (A∗I), the square identity matrix’s dimension should match the other matrix’s columns. When multiplying the identity matrix before some other matrix (I∗A), the square identity matrix’s dimension should match the other matrix’s rows .
1 | % Initialize random matrices A and B |
Inverse and Transpose
The inverse of a matrix $A$ is denoted $A^{−1}$. Multiplying by the inverse results in the identity matrix.
A non square matrix does not have an inverse matrix. We can compute inverses of matrices in octave with the pinv(A)
function and in Matlab with the inv(A)
function. Matrices that don’t have an inverse are singular or degenerate .
The transposition of a matrix is like rotating the matrix 90 ° in clockwise direction and then reversing it. We can compute transposition of matrices in matlab with the transpose(A) function or A'
:
$$
A = \begin{bmatrix} a & b \ c & d \ e & f \end{bmatrix}
$$
$$
A^T = \begin{bmatrix} a & c & e \ b & d & f \ \end{bmatrix}
$$
In other words:
$$
A_{ij} = A^T_{ji}
$$
1 | % Initialize matrix A |