Execute statements if condition is true
if evaluates an expression,
and executes a group of statements when the expression is true. An
expression is true when its result is nonempty and contains only nonzero
elements (logical or real numeric). Otherwise, the expression is false.
are optional. The statements execute only if previous expressions
if...end block are false. An
can include multiple
Create a matrix of 1s.
nrows = 4; ncols = 6; A = ones(nrows,ncols);
Loop through the matrix and assign each element a new value. Assign
2 on the main diagonal,
-1 on the adjacent diagonals, and
0 everywhere else.
for c = 1:ncols for r = 1:nrows if r == c A(r,c) = 2; elseif abs(r-c) == 1 A(r,c) = -1; else A(r,c) = 0; end end end A
A = 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0
Expressions that include relational operators on arrays, such as
A > 0, are true only when every element in the result is nonzero.
Test if any results are true using the
limit = 0.75; A = rand(10,1)
A = 0.8147 0.9058 0.1270 0.9134 0.6324 0.0975 0.2785 0.5469 0.9575 0.9649
if any(A > limit) disp('There is at least one value above the limit.') else disp('All values are below the limit.') end
There is at least one value above the limit.
Compare arrays using
isequal rather than the
== operator to test for equality, because
== results in an error when the arrays are different sizes.
Create two arrays.
A = ones(2,3); B = rand(3,4,5);
size(B) are the same, concatenate the arrays; otherwise, display a warning and return an empty array.
if isequal(size(A),size(B)) C = [A; B]; else disp('A and B are not the same size.') C = ; end
A and B are not the same size.
strcmp to compare strings. Using
test for equality results in an error when the strings are different
reply = input('Would you like to see an echo? (y/n): ','s'); if strcmp(reply,'y') disp(reply) end
Determine if a value falls within a specified range.
x = 10; minVal = 2; maxVal = 6; if (x >= minVal) && (x <= maxVal) disp('Value within specified range.') elseif (x > maxVal) disp('Value exceeds maximum value.') else disp('Value is below minimum value.') end
Value exceeds maximum value.
An expression can include relational operators
==) and logical
operators (such as
~). Use the logical operators
create compound expressions. MATLAB® evaluates compound expressions
from left to right, adhering to operator precedence rules.
Within the conditional expression of an
as short-circuit operators. This behavior is the same as
short-circuit in conditional expressions and statements, it is good
practice to use
| within the expression.
x = 42; if exist('myfunction.m','file') && (myfunction(x) >= pi) disp('Expressions are true') end
The first part of the expression evaluates to false. Therefore, MATLAB does not need to evaluate the second part of the expression, which would result in an undefined function error.
You can nest any number of
if statement requires an
Avoid adding a space after
elseif keyword (
The space creates a nested
if statement that requires