for loops and if conditionals in the first half of the course. logical variable type in MATLAB, is a variable with two possible values true and false. true or \(0\) for false. 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\). & 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\) |
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\). a = ['B', 'C', 'D']; and the correct answers are s = ['B', 'D', 'D'];. sum(a = s) sum(a == s) sum(a ~= s) sum(a != s) g = ['C', 'C', 'N', 'N'];. sum(1 * (g == 'C') + 0 * (g == 'N')) sum(1 * (g == 'N') + 0 * (g == 'C')) mean(1 * (g == 'C') + 0 * (g == 'N')) mean(1 * (g == 'N') + 0 * (g == 'C')) 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']. s(sum(g >= c)) s(sum(g < c)) s(sum(g >= c) + 1) s(sum(g < c) + 1) log has \(1\) input and \(1\) output, + has \(2\) inputs and \(1\) output, and size has \(1\) input and \(2\) outputs. .m files and used in commands. f.m. 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 line after % is not executed by the program. end, but it can be omitted. f.m, but only \(f\) can be used outside the file in commands. f.m that is not \(f\) are helper functions. function z = add(x, y) z = x + y end add(1, 2) returns \(3\). function v = f1(x) v = [x, x + 1]; end sum(f1(2)) function [u, v] = f2(x) u = x; v = x + 1; end sum(f2(2)) 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. function z = lincom(x, y, u, v) arguments x; y; u = 1; v = 1; end z = u * x + v * y; end lincom(1, 2, 1, 1) lincom(1, 2, 1) lincom(1, 2) lincom([1, 2]) Last Updated: September 11, 2025 at 10:54 PM