An interactive introduction to MATLAB

(Jeff_L) #1

46 decision making


Listings 4.1 presents a simple example of using relational operators.
Listing 4.1: Simple relational operators
1 >> x = 5;
2 >> y = 10;
3 >> x<y
4 ans =
5 1
6 >> x>y
7 ans =
8 0

Comments:


  • Lines 3 and 6 are called logical expression because the result can only be
    either true, represented by 1 , or false, represented by 0.


Listings 4.2–4.3 present more examples of using relational and logical operators.
Listing 4.2: Relational operators
1 >> x = [1 5 3 7];
2 >> y = [0 2 8 7];
3 >> k = x<y
4 k =
5 0 0 1 0
6 >> k = x<=y
7 k =
8 0 0 1 1
9 >> k = x>y
10 k =
11 1 1 0 0
12 >> k = x>=y
13 k =
14 1 1 0 1
15 >> k = x==y
16 k =
17 0 0 0 1
18 >> k = x~=y
19 k =
20 1 1 1 0

Listing 4.3: Logical operators
1 >> x = [1 5 3 7];
2 >> y = [0 2 8 7];
3 >> k = (x>y) & (x>4)
4 k =
5 0 1 0 0
6 >> k = (x>y) | (x>4)
7 k =
8 1 1 0 1
Free download pdf