MATLAB has relational operators, conditional statements, and loops. Relational operators are used to make comparisons. The conditional statements allow us
to write programs that make decisions. A loop is a structure for repeating a calculation a number of times. Conditional statements and loops are best used in a script file, rather than in an interactive session.
In this section we will limit our applications of relational operators, conditional statements, and loops to the use of scalar variables. We will show how to use arrays to make decisions and computations in loops.
MATLAB has six relational operators to make comparisons between arrays. These operators are shown in Table 1.6-1. Note that the “equal to” operator consists of two = signs, not a single = sign as you might expect. The single =sign is the assignment operator in MATLAB. T’:e result of a comparison using the relational operators is a logical value, which is either a 0 (if the comparison is false) or a I (if the comparison is true), and the result can be used as a logical variable, which is a variable containing logical values
For example, if x = 2 and y = 5, typing 2 = x < y returns the value 2 = 1, because x is less than y. Typing u = X= =v returns the value u = 0 because x does not equal y. To make the statements more readable, we can group the operations using parentheses. For example, 2 = (x < y) and u = (x= =v ) . For the operators consisting of two symbols, there cannot be a space between them. The relational operators compare arrays on an element-by-element basis. The arrays must have the same dimension. The only exception occurs when we compare an array to a scalar. In that case, all the elements of the array are compared to the scalar. For example, suppose that x = [6 , 3 , 9) and y = [ 14 , 2 , 9].
The following MATLAB session shows some examples.
» 2 = (x > y)
1 0 0
»z = ( x > y )
0 1 0
»z = ( x y )
1 1 0
» z = ( x — y )
0 0 1
» z = ( x > 8 )
We can also use the relational operators to address arrays. For example, with =[6,3,9,11] andy = [14,2,9,13] typing z = x ( x < y ) finds all the elements in x that are less than the corresponding elements in y. The result is the array z = [ 6 r 11].
The find Function We can use the find function to create decision-making programs, especially when we combine it with the relational operators. The function find (x ) computes an array containing the indices of the nonzero elements of the numeric array x. For example, consider the session.
»x = [-2,0,4];
»y = find (x)
The resulting array y = [ 1, 3] indicates that the first and third elements of x are nonzero. Note that the find function returns the indices. not the values. In the following session, note the difference between the result obtained by x ( x < y ) and the result obtained by find ( x < y ).
Thus two values in the array x are less than the corresponding values in the array y. They are the first and fourth values, 6 and II. To find out how many, we could also have typed length (indices).
In the above example, there were only a few numbers in the arrays x and y, and thus we could have obtained the answers by visual inspection. However, these MATLAB methods are useful either where there is so much data that visual inspection would be time-consuming, or where the values are generated by a program.
The MATLAB conditional statements allow us to write programs that make decisions. Conditional statements contain one or more of the if, e1s e, and e1s e if statements. The end statement denotes the end of a conditional statement. These conditional statements read somewhat like their English language equivalents. For example suppose that x is a scalar, and that we want to compute y = √x only if x ≥ o. In English, we could specify this as: If x is greater than or equal to zero, compute y from y = √x, otherwise, do nothing. The if statement in the following script file accomplishes this in MATLAB, assuming that the variable x already has a scalar value.
if x >= 0
y = sqrt (x)
If x is negative, the program takes no action.
When more than one action must occur as a result of a decision, we can use the else and elseif statements. The statements after the else are executed if all the preceding if and elseif expressions are false. The general form of the if statement is
The else and elseif statements may be omitted if not required.
Suppose that we want to compute y from y = √x for x ≥ 0, and y = – √-x for x < 0. The following statements will calculate y, assuming that the variable x already has a scalar value.
As another example, suppose that we want to compute y such that
The following statements will compute v, assuming that the variable x already has a scalar value.
if x >= 9
y = 15 * sqrt (4x) = 9
elseif x > = 0
y = 10*x + 10
y = 10
Note that the e 1 5 e if statement does not require a separate end statement. When the variable being tested (x in the previous session) is an array rather than a scalar, the if-elseif-else-end structure can give unexpected results if not used carefully. This is discussed.
A loop is a structure for repeating a calculation a number of times. Each repetition of the loop is a pass. There are two types of explicit loops in MATLAB: the for loop, used when the number of passes is known ahead of time, and the while loop, used when the looping process must terminate when a specified condition is satisfied, and thus the number of passes is not known in advance.
A simple example of a for loop is
m = 0 ;
x ( 1 ) = 10 ;
for k = 2 : 3 : 11
m = m + l ;
x ( m + l ) = x ( m ) + k∧2 ;
The loop variable k is initially assigned the value 2. During each successive pass through the loop k is incremented by 3, and x is calculated until k exceeds II. Thus, k takes on the values 2, 5, 8, 11. The variable m indicates the indea of the array x. The program then continues to execute any statements following the end statement. When the loop is finished the array x will have the values x (1) =14, x (2) =39, x (3) =103. x (4) =224. The name of the loop variable need not be k.
The while loop is used in cases where the looping process must terminate when a specified condition is satisfied, and thus the number of passes is not known in advance. A simple example of a while loop is
x = 5 ;
k = 0 ;
while x < 25
k = k + 1 ;
y (k) = : 3 * x ;
x =: 2 * x – 1 ;
The loop variable x is initially assigned the value 5, and it keeps this value until the statement x = 2 *x – 1 is encountered the first time. Its value then changes to 9. Before each pass through the loop. x is checked to see if its value is less than 25. If so, the pass is made. If not. the loop is skipped and the program continues 10 execute any statements following the end statement. The variable x takes on the values 9, 17, and 33 within the loop. The resulting array y contains the values y (l) = 15,y(2) = 27,y(:3) = 51.
The Editor/Debugger is capable of automatically indenting to improve the readability of a file. For example, if, else, elseif, for, and while structures do not require indenting, but doing so enables the reader to identify the structure more easily. The Editor/Debugger automatically indents the lines after if, else, elseif, for, and while statements when you press the Enter key. It continues to indent until the corresponding end statement is reached. It also uses syntax highlighting to identify key statements by displaying them in different colors.
Table 1.6-2 summarizes these statements covers these topics in greater depth, and also introduces logical (Boolean) operators and string variables. More information on script files, function files, and the Editor/Debugger is given.