본문 바로가기

BITS

[Machine Learning by Stanford] Linear Algebra Review - Matrices and Vectors

This is a brief summary of ML course provided by Andrew Ng and Stanford in Coursera.

You can find the lecture video and additional materials in 

https://www.coursera.org/learn/machine-learning/home/welcome

 

Coursera | Online Courses From Top Universities. Join for Free

1000+ courses from schools like Stanford and Yale - no application required. Build career skills in data science, computer science, business, and more.

www.coursera.org

Matrix: Rectangular array of numbers: 

$\mathbf{X} = \left[\begin{array} {rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right] $

$\mathbf{X} = \left[\begin{array} {rrr} 1401 & 191  \\ 1371 & 821 \\ 949 & 1437 \\ 147 & 1448 \end{array}\right] $

 

Dimension of matrix: number of rows x number of columns

3X3 Matrix $(R^{3x3})$ and 4X2 $(R^{4x2})$ Matrix above. 

 

QuizWhich of the following statements are true? Check all that apply.

1. $\mathbf{X} = \left[\begin{array} {rrr} 1 & 2 \\ 4 & 0 \\ 0 & 1 \end{array}\right] $ is a 3 X 2 matrix. 

2. $\mathbf{X} = \left[\begin{array} {rrr} 0 & 1 & 4 & 2 \\ 3 & 4 & 0 & 9 \end{array}\right] $ is a 4 X 2 matrix.

3. $\mathbf{X} = \left[\begin{array} {rrr} 0 & 4 & 2 \\ 3 & 4 & 9 \\ 5 & -1 & 0 \end{array}\right] $ is a 3 X 3 matrix.

4. $\mathbf{X} = \left[\begin{array} {rrr} 1 & 2 \end{array}\right] $ is a 1 X 2 matrix.

 

Answer: 1, 3, 4

 

Matrix Elements (entries of matrix)

$\mathbf{A} = \left[\begin{array} {rrr} 1401 & 191  \\ 1371 & 821 \\ 949 & 1437 \\ 147 & 1448 \end{array}\right] $

$A_{ij}$= "i,j entry" in the $i^{th}$ row, $j^{th}$ column.

$A_{11}$ = 1402, 

$A_{12}$ = 191, 

$A_{32}$ = 1437, 

$A_{41}$ = 147,

$A_{43}$ = undefined (error)

 

 

Quiz: Let A be a matrix shown below. $A_32$ is one of the elements of this matrix.

$\mathbf{A} = \left[\begin{array} {rrr} 85 & 76 & 66 & 5 \\ 94 & 75 & 18 & 28 \\ 68 & 40 & 71 & 5 \end{array}\right] $ 

What is the value of $A_{32}$ ?

Answer: 40

 

Vector: An n X 1 matrix (1 column)

$\mathbf{X} = \left[\begin{array} {rrr} 460 \\ 232 \\ 315 \\ 178 \end{array}\right] $ -> $R^4$

This is 4-dimensional vector. 

$y_i = i^{th}$ element

$y_1$ = 460, $y_2$ = 232, $y_3$ = 315

Othen than explicitly specified, use 1-indexed vector. 

And use capital letter for matrices, and lower case to refer to either numbers, scalar, or vectors. 

 

Lecturer's Note

Matrices are 2-dimensional arrays:

$\mathbf{A} = \left[\begin{array} {rrr} a & b & c \\ d & e & f \\ g & h & i \\  j & k & l \end{array}\right] $ 

The above matrix has four rows and three columns, so it is a 4 x 3 matrix. 

A vector is a matrix with one column and many rows:

 

$\mathbf{A} = \left[\begin{array} {rrr} w \\ x \\ y \\ z \end{array}\right] $ 

So vectors are a subset of matrices. The above vector is a 4 x 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.
  • R refers to the set of scalar real numbers.
  • $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.

% The ; denotes we are going back to a new row.
A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]

% Initialize a vector 
v = [1;2;3] 

% Get the dimension of the matrix A where m = rows and n = columns
[m,n] = size(A)

% You could also store it this way
dim_A = size(A)

% Get the dimension of the vector v 
dim_v = size(v)

% Now let's index into the 2nd row 3rd column of matrix A
A_23 = A(2,3)

 

A = 1 2 3 4 5 6 7 8 9 10 11 12 v = 1 2 3 m = 4 n = 3 dim_A = 4 3 dim_v = 3 1 A_23 = 6