Prev: W9 Next: W11

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

Slide:


# Vectorization, Again, Again

📗 If the same task if performed for different values of a variable, use an indicator variable and vectorize.
📗 If different tasks are performed for different values of a variable, use a switch conditional.
➩ Different tasks are performed for \(x = v_{1}\), for \(x = v_{2}\) or \(v_{3}\), and for every other value of \(x\).
switch x
  case v1
  ...
  case {v2, v3}
  ...
  otherwise
  ...
end
📗 If different tasks are performed under different conditions, use an if conditional.
➩ Different task are performed if \(x \neq 0\), if \(x = 0\) but \(y \neq 0\), and if \(x = 0\) and \(y = 0\).
if x
  ...
elseif y
  ...
else
  ...
end

# Condition for If

📗 if x and if x ~= 0 represent the same condition. The expression x ~= 0 should be treated as a variable whose value is \(\begin{cases} 1 & \text{if} x \neq 0 \\ 0 & \text{if} x = 0 \\ \end{cases}\).
📗 while x and while x ~= 0 represent the same loop for the same reason.
TopHat Quiz
📗 x = 10; switch mod(x, 4)
📗   case 0
📗     x + 1
📗   case {1, 2}
📗     x * 2
📗   otherwise
📗     x ^ 3
📗 end
➩ A: \(10\)
➩ B: \(11\)
➩ C: \(20\)
➩ D: \(1000\)

TopHat Quiz
📗 x = 10
📗 if x < 10 && ~mod(x, 2)
📗   x + 1
📗 elseif ~mod(x , 3)
📗   x * 2
📗 else
📗   x ^ 3
📗 end
➩ A: \(10\)
➩ B: \(11\)
➩ C: \(20\)
➩ D: \(1000\)


# Number of Input Arguments

📗 When the function function z = f(x, y) is called, \(0\), \(1\), or \(2\) arguments can be provided.
switch can be used here to perform different tasks when different number of arguments are given.
nargin is the number of input arguments provided when the function is called.
📗 varargin represents an arbitrary number of input variables.
➩ It can only be used as the last argument of a function, for example, function y = f(x1, x2, x3, varargin).
➩ The \(i\)-th argument can be accessed by varargin{i}.
📗 varargout represents an arbitrary number of output variables.
nargout represents the number of output variables assigned when the function is called.
➩ For example, x = size([1 2; 3 4]) assigns \(x\) the value \(\begin{bmatrix} 2 & 2 \end{bmatrix}\) and [x, y] = size([1 2; 3 4]) assigns \(x\) the value \(2\).

# Variable Input Example

📗 For example, a new log function can be defined by log() returns \(1\), log(x) returns natural log \(\log\left(x\right)\), and log(x, n) returns \(\log_{n}\left(x\right)\).
function z = log(x, y)
  switch nargin
    case 1
      z = log(x);
    case 2
      z = log(x) / log(y);
    otherwise
      z = 1;
  end
end
📗 Another example, a new log function can be defined so that it returns a vector if more than one input is provided.
function z = log(x, varargin)
  if nargin == 1
    z = log(x);
  else
    z = [log(x) zeros(1, nargin - 1)]
    for t = 2:nargin
      z(t) = log(varagin{t - 1})
    end
  end
end

# Recursion

📗 A function that calls itself is a recursive function.
function z = f(x)
if x ... base case
  z = ...
else recursion
  z = ... f(x') ...
end
📗 For example, factorial can be computed using recursion (it should not).
function z = f(x)
  if x == 0
     z = 1;
  else
     z = x * f(x - 1);
  end
end
TopHat Quiz
📗 function z = fib(x)
📗   if x < 3
📗     z = 1;
📗   else
📗     z = fib(x - 1) + fib(x - 2)
📗   end
📗 end
📗 fib(5)
➩ A: 2
➩ B: 3
➩ C: 5
➩ D: 7

TopHat Quiz
📗 function z = combin(x)
📗   if y == 0 || y == x
📗     z = 1;
📗   else
📗     z = combin(x - 1, y) + combin(x - 1, y - 1);
📗   end
📗 end
📗 combin(3, 2)
➩ A: 2
➩ B: 3
➩ C: 6
➩ D: 9


📗 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: April 18, 2025 at 6:18 PM