while
loop can be used to keep asking for user input until the input is valid. valid = 0;
while ~valid
x = input(...)
if ...
check valid valid = 1
end
end
isletter(x)
or isstrprop(x, 'alpha')
checks if the string \(x\) contains characters that are letters 'a'
to 'z'
or 'A'
to 'Z'
. isstrprop(x, 'digit')
checks if the string \(x\) contains characters that are number '0'
to '9'
ismember(x, y)
checks if the string \(x\) contains characters that are characters in the other string \(y\). contains(x, y)
checks if string \(x\) contains a substring \(y\). count(x, y)
counts the number of occurrences of \(y\) in the string \(x\). startsWith(x, y)
and endsWith(x, y)
checks if the string \(x\) starts with or ends with the substring \(y\). s = 'abc'; t = 'cde';
. contains(s, t)
ismember(s, t)
max(ismember(s, t))
min(ismember(s, t))
s = 'abc';
. sum(isletter(s) + isstrprop(s, 'digit')) >= 3
sum(isletter(s) >= 2 + isstrprop(s, 'digit') >= 1) >= 3
sum(isletter(s)) >= 2 | sum(isstrprop(s, 'digit')) >= 1
sum(isletter(s)) >= 2 & sum(isstrprop(s, 'digit')) >= 1
s = 'aacc'; t = 'abbc'
. sum(ismember(s, t)) == length(s)
sum(ismember(s, t)) == length(t)
sum(sort(s) == sort(t)) == length(s)
sum(sort(s) == sort(t)) == length(t)
function f(x)
arguments
x (size) class {function} = default value
end
...
actual function end
x (size) class {functions}
, the size is specified by a comma-separated list. x (n, m) ...
requires \(x\) to be an \(n \times m\) matrix. x (n, :) ...
requires \(x\) to be a matrix with \(n\) rows or a vector with \(n\) elements. x (:, m) ...
requires \(x\) to be a matrix with \(m\) columns. x (size) class {functions}
, the class is specified by its class name. x (...) char ...
and x (...) string ...
require \(x\) to be a string. x (...) single ...
and x (...) double ...
require \(x\) to be a number. x (...) logical ...
requires \(x\) to be a Boolean variable. x (size) class {functions}
, the functions are special functions that raise an error when some conditions are not satisfied: Link. x ... {mustBeGreaterThanOrEqual(x, l), mustBeLessThanOrEqual(x, u)}
requires \(x\) to be between \(l\) and \(u\), and raises an error when \(x < l\) or \(x > u\). x ... {mustBeMember(x, [u, v, w])}
requires \(x\) to be one of \(u\) or \(v\) or \(w\). y = @f
creates a variable \(y\) that represents the function \(f\). The variable \(y\) is a function handle. y = @(x) f(x)
creates an anonymous function and stores it in the variable named \(y\). function mfg = maxFun(f, g, x)
mfg = max(f(x), g(x));
end
maxFun(@sin, @(x)(sin(2 * x)), 1)
finds the maximum between \(\sin\left(1\right)\) and \(\sin\left(2 \cdot 1\right)\). function mfg = maxFun(f, g)
mfg = @(x)(max(f(x), g(x)));
end
h = maxFun(@sin, @(x)(sin(2 * x)))
, then h(1)
finds the maximum between \(\sin\left(1\right)\) and \(\sin\left(2 \cdot 1\right)\). function h = noise(f, g, v)
h = @(x)(f(x) + v * g(x))
end
h = noise(@sqrt, @(x)(x .^ 2), 0.5);
h(4)
function h = noise(f, g)
h = @(x)(f(x) + rand() * g(x))
end
h = noise(@sqrt, @(x)(x .^ 2));
h(4) == h(4)
Last Updated: April 18, 2025 at 6:18 PM