Prev: W4 Next: W6

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

Slide:


# Comment on Vectorization

📗 Try to avoid using for loops and if conditionals in the first half of the course.
📗 The main difference between MATLAB and other programming languages is its very efficient matrix operation implementation.

# Boolean Variables and Indicator Functions

📗 A Boolean variable, also called logical variable type in MATLAB, is a variable with two possible values true and false.
📗 A Boolean variable is stored as either \(1\) for true or \(0\) for false.
📗 Indicator functions, also called dummy variables, are functions that return \(1\) if a condition is satisfied and \(0\) if the condition is not satisfied.
x == y is the indicator if \(x = y\), meaning \(\begin{cases} 1 & \text{if} x = y \\ 0 & \text{if} x \neq y \\ \end{cases}\).
x ~= y is the indicator if \(x \neq y\), meaning \(\begin{cases} 1 & \text{if} x \neq y \\ 0 & \text{if} x = y \\ \end{cases}\), again x != y does not work in MATLAB.
x > y, x >= y are indicators of \(x > y\) and \(x \geq y\).
x < y, x <= y are indicators of \(x < y\) and \(x \leq y\).
~ is not: ~0 is \(1\) and ~1 is \(0\).
📗 These functions can be applied element-wise to a vector directly.

# Short Circuit Evaluation

📗 & is and, | is or, and can be applied element-wise to a vector.
📗 && is and, || is or, but only works on scalars. && and || use short-circuit evaluation, for example, when evaluating a && b, if \(a\) is false, then \(b\) will not be evaluated, and when evaluating a || b, if \(a\) is true, then \(b\) will not be evaluated.
a b a & b, a && b a | b, a || b
\(0\) \(0\) \(0\) \(0\)
\(0\) \(1\) \(0\) \(1\)
\(1\) \(0\) \(0\) \(1\)
\(1\) \(1\) \(1\) \(1\)


# Vector Reduction Logical Functions

📗 Some functions reduce a vector into a scalar:
any(x) returns whether any of the elements in the matrix or vector \(x\) is non-zero.
all(x) returns whether all of the elements in the matrix or vector \(x\) are non-zero.
find(x) finds the indices of all the non-zero elements in the vector \(x\).
find(x, 1) finds the index of the first non-zero elements in the vector \(x\).
sum(x) and prod(x) compute the sum and product of the elements in a matrix or vector \(x\).
sum(x, 1) and prod(x, 1) compute the column sums and products of the elements in a matrix \(x\), for example, sum([1 2; 3 4], 1) returns the column sums \(\begin{bmatrix} 4 & 6 \end{bmatrix}\).
sum(x, 2) and prod(x, 2) compute the row sums and products of the elements in a matrix \(x\), for example, sum([1 2; 3 4], 2) returns the column sums \(\begin{bmatrix} 3 \\ 7 \end{bmatrix}\).
mean(x) computes the average of the numbers in a matrix or vector \(x\).
max(x) and min(x) compute the maximum and minimum of the elements in a matrix or vector \(x\).
TopHat Quiz
📗 Compute the number of questions a student gets incorrect if the student's answers are a = ['B', 'C', 'D']; and the correct answers are s = ['B', 'D', 'D'];.
➩ A: sum(a = s)
➩ B: sum(a == s)
➩ C: sum(a ~= s)
➩ D: sum(a != s)

TopHat Quiz
📗 Compute the GPA if \(C\) is worth \(1\) point and \(N\) is worth \(0\) point for a student whose grades are g = ['C', 'C', 'N', 'N'];.
➩ A: sum(1 * (g == 'C') + 0 * (g == 'N'))
➩ B: sum(1 * (g == 'N') + 0 * (g == 'C'))
➩ C: mean(1 * (g == 'C') + 0 * (g == 'N'))
➩ D: mean(1 * (g == 'N') + 0 * (g == 'C'))

TopHat Quiz
📗 Compute letter grade of g = 75; if \(A\) corresponds to a grade \(\geq 90\), \(B\) for a grade \(\geq 80\), \(C\) for a grade \(\geq 70\), and \(D\) otherwise. Define c = [90, 80, 70, 0]' and s = ['A', 'B', 'C', 'D'].
➩ A: s(sum(g >= c))
➩ B: s(sum(g < c))
➩ C: s(sum(g >= c) + 1)
➩ D: s(sum(g < c) + 1)


# Functions

📗 A function \(y = f\left(x\right)\) is a mapping from a list of inputs \(x\), also called arguments or parameters, to a list of outputs \(y\).
📗 MATLAB has many built-in functions, for example, log has \(1\) input and \(1\) output, + has \(2\) inputs and \(1\) output, and size has \(1\) input and \(2\) outputs.
📗 New functions can be defined in .m files and used in commands.
📗 A function with name \(f\) should be put in a file named f.m.
➩ The first line of the file is function y = f(x) or function [y1, y2, ...] = f(x1, x2, ...), where \(y\) is the name or names of the output variables to return, and \(x\) is the list of arguments of the function.
➩ The second line of the file is usually comments describing what the function does. Comments start with % the line after % is not executed by the program.
➩ The last line of the file should be end, but it can be omitted.
📗 Multiple functions can be defined in the same file f.m, but only \(f\) can be used outside the file in commands.
➩ The functions in f.m that is not \(f\) are helper functions.

# Operator Example

📗 The addition function \(x + y\) is usually written in the infix notation (first argument, then function name, then argument 2).
📗 The following function is the addition function in prefix notation (function name, then first argument, then second argument).
function z = add(x, y)
  z = x + y
end
📗 add(1, 2) returns \(3\).
TopHat Quiz
📗 function v = f1(x)
📗   v = [x, x + 1];
📗 end
📗 sum(f1(2))
➩ A: \(2\)
➩ B: \(3\)
➩ C: \(5\)
➩ D: \(\begin{bmatrix} 2 & 3 \end{bmatrix}\)

TopHat Quiz
📗 function [u, v] = f2(x)
📗   u = x; v = x + 1;
📗 end
📗 sum(f2(2))
➩ A: \(2\)
➩ B: \(3\)
➩ C: \(5\)
➩ D: \(\begin{bmatrix} 2 & 3 \end{bmatrix}\)


# Default Value Example

📗 The linear interpolation of two vectors \(x\) and \(y\) at some \(u \in \left[0, 1\right]\) is \(\left(1 - u\right) x + u y\).
📗 Sometimes, \(u\) is not specified, so the default value \(u = 0.5\) (mid-point) is used.
function z = lerp(x, y, u)
  arguments
    x; y; u = 1;
  end
  z = (1 - u) * x + u * y
end
📗 lerp(1, 3, 0.5) and lerp(1, 3) return \(2\), but lerp(1) leads to an error.
TopHat Quiz
📗 function z = lincom(x, y, u, v)
📗   arguments
📗     x; y; u = 1; v = 1;
📗   end
📗   z = u * x + v * y;
📗 end
📗 Which of the following is not computing the sum?
➩ A: lincom(1, 2, 1, 1)
➩ B: lincom(1, 2, 1)
➩ C: lincom(1, 2)
➩ D: lincom([1, 2])


📗 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