| Contents | Index |
if expression statements elseif expression statements else statements end
An if statement evaluates an expression, and executes a group of statements when the expression is true.
elseif and else are optional, and execute only when previous expressions in the if statement are false.
For more information about valid expressions and when they are true, see the if reference page.
Avoid adding a space within the elseif keyword (else if). The space creates a nested if statement that requires its own end keyword.
Assign to a matrix values that depend on their indices:
% Preallocate a matrix
nrows = 10;
ncols = 10;
myData = ones(nrows, ncols);
% Loop through the matrix
for r = 1:nrows
for c = 1:ncols
if r == c
myData(r,c) = 2;
elseif abs(r - c) == 1
myData(r,c) = -1;
else
myData(r,c) = 0;
end
end
endend | for | if | switch | while

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |