4.1relational and logical operations 47
9 >> k = ~((x>y) | (x>4))
10 k =
11 0 0 1 0
Comments:
- The relational and logical operators are used to compare, element-by-
element, the vectorsxandy. - The result of each comparison is a logical vector i. e.konly contains 1’s
and 0’s (corresponding to true or false).
Single and double equals signs
The difference between=and==is often misunderstood. A single equals sign
is used to assign a value to a variable e. g.x=5. A double equals sign is used to
test whether a variable is equal to given value e. g.my_test=(x==5)means
test ifxis equal to 5, and if so assign the value 1 (true) tomy_test.
Self Test Exercise: Relational operators and logical
- †Evaluate the following expressions without usingMATLAB. Check your
answer withMATLAB.
a)14 > 15/3
b) y=8/2 < 5× 3 +1 > 9
c)y=8/(2 < 5)× 3 + (1 > 9)
d) 2 + 4 × 3 ∼=60/4− 1 - †Given:a=4,b=7. Evaluate the following expressions without usingMAT-
LAB. Check your answer withMATLAB.
a)y=a+b >=a×b
b) y=a+ (b >=a)×b
c)y=b−a < a < a/b - †Given:v=[4 − 2 −1 5 0 1 −3 8 2], andw=[0 2 1 −1 0 −2 4 3 2].
Evaluate the following expressions without usingMATLAB. Check your
answer withMATLAB.
a)v <=w
†Question adapted from Gilat, A. (2008).MATLAB : An Introduction With Applications. John
Wiley & Sons, Inc., 3rd edition. Copyright©2008 John Wiley & Sons, Inc. and reprinted
with permission of John Wiley & Sons, Inc.