Prev: W1 Next: W2

📗 Tuesday lectures: 4:00 to 4:50, Zoom, TopHat: Link (or Google Form: Form if TopHat not working). MATLAB.
📗 Programming Homework: P1

Slide:


# What and Why MATLAB

📗 Matrix Laboratory: mainly used for numerical matrix computations.
➩ Numerical: approximation of continuous functions.
➩ Matrix: rectangular 2D array of numbers.
📗 Matrix operations are simple to code.
📗 Matrix operations are very fast.
TopHat Discussion
📗 Why are you taking this course?

# How to Open MATLAB

📗 Download MATLAB from Link or use the online version (preferred): Link.
📗 Command Window executes commands line by line.
📗 Text Editor creates an m-file script used to store a series of commands or to define functions.
📗 Current Folder lists the files in the working directory.
📗 Workspace lists the variables defined in the current session.
TopHat Quiz
📗 (1 + 10^-16 - 1) * 10^16
➩ A: \(0\)
➩ B: \(1\)
➩ C: something else
TopHat Quiz
📗 (1 + 10^-15 - 1) * 10^15
➩ A: \(0\)
➩ B: \(1\)
➩ C: something else


# MATLAB Variables

📗 Every variable in MATLAB is a matrix.
➩ A scalar is a \(1\) by \(1\) matrix.
➩ A column vector is an \(N\) by \(1\) matrix.
➩ A row vector is a \(1\) by \(N\) matrix.

# Matrix Creation

📗 [a; b] creates the matrix (column vector) \(\begin{bmatrix} a \\ b \end{bmatrix}\).
📗 [a b] or [a, b] creates the matrix (row vector) \(\begin{bmatrix} a & b \end{bmatrix}\).
📗 [a b; c d] creates the matrix \(\begin{bmatrix} a & b \\ c & d \end{bmatrix}\).
➩ \(a, b, c, d\) can be (sub)matrices themselves.

# Vector Creation Shortcuts

📗 a:b creates the matrix (row vector) \(\begin{bmatrix} a & a + 1 & a + 2 & ... & b \end{bmatrix}\).
📗 a:d:b creates the matrix (row vector) \(\begin{bmatrix} a & a + d & a + 2d & ... & b \end{bmatrix}\).
➩ If \(b \neq a + d n\) for some \(n\), then the list stops at the largest value of \(a + d n\) that is less than \(b\).
TopHat Quiz
📗 [1:2:4 4:2:1]
➩ A: \(\begin{bmatrix} 1 & 3 & 4 & 2 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 1 & 3 \\ 4 & 2 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 1 & 3 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 1 & 3 & 4 \end{bmatrix}\)


# Matrix Creation Shortcuts

📗 zeros(n, m) creates an \(n\) by \(m\) matrix of \(0\)s (\(n\) rows and \(m\) columns).
📗 ones(n, m) creates an \(n\) by \(n\) matrix of \(1\)s (\(n\) rows and \(m\) columns).
📗 repmat(x, n, m) repeats the scalar or matrix \(x\), \(n\) by \(m\) times.
📗 eye(n) creates an \(n\) by \(n\) identity matrix, for example, \(\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}\) when \(n = 3\).
📗 diag([a b c]) creates a diagonal matrix \(\begin{bmatrix} a & 0 & 0 \\ 0 & b & 0 \\ 0 & 0 & c \end{bmatrix}\).
TopHat Quiz
📗 [eye(1) ones(1, 2); zeros(2, 1) diag(1:2)]
➩ A: \(\begin{bmatrix} 0 & 1 & 1 \\ 1 & 1 & 0 \\ 1 & 0 & 2 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 0 & 0 & 0 \\ 1 & 1 & 0 \\ 1 & 0 & 2 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 1 & 1 & 1 \\ 0 & 1 & 0 \\ 0 & 0 & 2 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 1 & 0 & 0 \\ 1 & 1 & 0 \\ 1 & 0 & 2 \end{bmatrix}\)

TopHat Quiz
📗 repmat([1 2], 2, 1)
➩ A: \(\begin{bmatrix} 1 & 2 & 1 & 2 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 1 \\ 2 \\ 1 \\ 2 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 1 & 2 \\ 1 & 2 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 1 & 1 \\ 2 & 2 \end{bmatrix}\)


# Transpose

📗 Transposing a matrix rearranges the elements of the matrix so that columns become rows and rows become columns.
[a b; c d]' produces the transpose \(\begin{bmatrix} a & b \\ c & d \end{bmatrix} ^\top = \begin{bmatrix} a & c \\ b & d \end{bmatrix}\).
[a b]' produces the column vector \(\begin{bmatrix} a & b \end{bmatrix} ^\top = \begin{bmatrix} a \\ b \end{bmatrix}\).
[a; b]' produces the row vector \(\begin{bmatrix} a \\ b \end{bmatrix} ^\top = \begin{bmatrix} a & b \end{bmatrix}\).

# Matrix Scalar Operations

📗 Suppose \(M\) is a matrix and \(c\) is a scalar.
M + c adds \(c\) to every element of \(M\), for example, zeros(n, m) + 1 produces the same matrix as ones(n, m).
M * c multiplies \(c\) to every element of \(M\), for example, ones(n, m) * 0 produces the same matrix as zeros(n, m).

# Vector Access

📗 Suppose \(M\) is a row vector.
➩ If \(x\) is a scalar, M(x) accesses the \(x\)-th element of \(M\).
➩ If \(x\) is a row vector, M(x) accesses the (sub)vector of \(M\) containing elements with indices in \(x\).
TopHat Quiz
📗 M = [5 4 3 2 1]; M([5 1])
➩ A: \(\begin{bmatrix} 1 & 5 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 5 & 1 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 1 \\ 5 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 5 \\ 1 \end{bmatrix}\)

TopHat Quiz
📗 M = [1 2 3 4 5]; M([1:2 5:-1:4])
➩ A: \(\begin{bmatrix} 1 & 2 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 1 & 2 & 5 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 1 \\ 2 \\ 5 \\ 4 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 1 \\ 2 \\ 5 \\ 5 \\ 4 \end{bmatrix}\)


# Matrix Access

📗 Suppose \(M\) is a matrix.
➩ If \(x, y\) are scalars, M(x, y) accesses row \(x\) column \(y\) of \(M\).
➩ If \(x, y\) are vectors, M(x, y) accesses the (sub)matrix of \(M\) containing rows with indices in \(x\) and columns with indices in \(y\).
📗 Suppose \(M\) is a matrix.
➩ If \(x\) is a scalar, M(x, :) or M(x, 1:end) accesses row \(x\) of \(M\).
➩ If \(x\) is a vector, M(x, :) or M(x, 1:end) accesses the (sub)matrix of \(M\) containing rows with indices in \(x\).
📗 Suppose \(M\) is a matrix.
➩ If \(y\) is a scalar, M(:, y) or M(1:end, y) accesses row \(y\) of \(M\).
➩ If \(y\) is a vector, M(:, y) or M(1:end, y) accesses the (sub)matrix of \(M\) containing columns with indices in \(y\).
TopHat Quiz
📗 M = [1 2 3; 4 5 6; 7 8 9]; M(1, [3 2 1])
➩ A: \(\begin{bmatrix} 1 & 2 & 3 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 3 & 2 & 1 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 1 \\ 4 \\ 7 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 7 \\ 4 \\ 1 \end{bmatrix}\)

TopHat Quiz
📗 M = [1 2 3; 4 5 6; 7 8 9]; M(1:2, 3:-1:2)
➩ A: \(\begin{bmatrix} 1 & 2 \\ 4 & 5 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 2 & 3 \\ 5 & 6 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 3 & 2 \\ 6 & 5 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 7 & 8 \\ 4 & 5 \end{bmatrix}\)



📗 Notes and code adapted from the course taught by Professors Beck Hasti and Michael O'Neill.
📗 You can expand all TopHat Quizzes and Discussions: .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google form Form at the end of the lecture.





Last Updated: March 03, 2025 at 12:52 AM