MATLAB Function Reference | ![]() ![]() |
Conditionally execute statements
Syntax
ifexpression
statements
end ifexpression1
statements
elseifexpression2
statements
elsestatements
end
Description
if
conditionally executes statements.
ifexpression
statements
end
More complicated forms use else
or elseif
. Each if
must be paired with a matching end
.
Arguments
Examples
Here is an example showing if
, else
, and elseif
:
for i = 1:n for j = 1:n if i == j a(i,j) = 2; elseif abs([i j]) == 1 a(i,j) = 1; else a(i,j) = 0; end end end
Such expressions are evaluated as false unless every element-wise comparison evaluates as true. Thus, given matrices A
and B
:
A = B = 1 0 1 1 2 3 3 4
|
||
A < (B+1) |
Since no element of |
|
A & B |
||
|
See Also
break
, else
, end
, for
, return
, switch
, while
![]() | i | ifft | ![]() |