Matlab 4 Slide
Matlab 4 Slide
Chapter 4
Sequential operations: Instructions executed in order.
Programming with MATLAB Conditional operations: Control structures that first
ask a question to be answered with a true/false answer
and then select the next instruction based on the answer.
Iterative operations (loops): Control structures that
repeat the execution of a block of instructions.
Z.R.K
Z.R.K 4-2 Z.R.K
for Loops Note the following rules when using for loops
A simple example of a for loop is with the loop variable expression k = m:s:n
for k = 5:10:35
x = k^2 The step value s may be negative.
end Example: k = 10:-2:4 produces k = 10, 8, 6, 4.
The loop variable k is initially If s is omitted, the step value defaults to 1.
assigned the value 5, and x is If s is positive, the loop will not be executed if m
calculated from x = k^2. Each is greater than n.
successive pass through the loop
increments k by 10 and
If s is negative, the loop will not be executed if m
calculates x until k exceeds 35.
is less than n.
Thus k takes on the values 5, 15, If m equals n, the loop will be executed only
25, and 35, and x takes on the once.
values 25, 225, 625, and 1225. The If the step value s is not an integer, round-off
program then continues to errors can cause the loop to execute a different
execute any statements following Flowchart of a for
Loop. Figure 4.5–1
number of passes than intended.
the end statement.
4-24 Z.R.K 4-25 Z.R.K
Use of a Mask
The continue Statement One can often avoid the use of loops and branching and thus
create simpler and faster programs by using a logical array as a
The following code uses a continue statement to mask that selects elements of another array. Any elements not
avoid computing the logarithm of a negative number. selected will remain unchanged. The following session creates
the logical matrix C from the numeric matrix A .
x = [10,1000,-10,100]; >>A = [0, -1, 4; 9, -14, 25; -34, 49, 64];
y = NaN*x; >>C = (A >= 0);
1 0 1
for k = 1:length(x) The result is 1 0 1
C=
if x(k) < 0 0 1 1
continue One can use this mask technique to compute the square root of
only those elements of A given in the previous program that are
end not less than 0 and add 50 to those elements that are negative.
y(k) = log10(x(k)); The program is:
end A = [0, -1, 4; 9, -14, 25; -34, 49, 64];
C = (A >= 0);
y
A(C) = sqrt(A(C))
The result is y = 1, 3, NaN, 2. A(~C) = A(~C) + 50
4-26 Z.R.K More? See pages 210-217. 4-27 Z.R.K
The switch Structure The following switch block displays the point
The switch structure provides an alternative to using the on the compass that corresponds to that angle.
if, elseif, and else commands. Anything programmed switch angle
using switch can also be programmed using if structures.
case 45
However, for some applications the switch structure is
more readable than code using the if structure. disp(’Northeast’)
switch input expression % which can be a scalar or string case 135
case value1 disp(’Northwest’)
statement group 1 case 225
case value2 disp(’Southwest’)
statement group 2 case 315
.
.
.
disp(’Southeast’)
otherwise otherwise
statement group n disp(’Direction Unknown’)
end end
4-32 Z.R.K
4-33 Z.R.K More? See pages 225-227.
End of Chapter 4