Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational and Logical Operators EE 201 1C7-2 Spring 2012.

Similar presentations


Presentation on theme: "Relational and Logical Operators EE 201 1C7-2 Spring 2012."— Presentation transcript:

1 Relational and Logical Operators EE 201 1C7-2 Spring 2012

2  Achieve Comprehension LOL of using Relational and Logical Operators in MATLAB. Class Learning Objectives 2 C7-2 Spring 2012

3 String  A string is a variable that contains characters.  Strings are useful for creating input prompts and messages and for storing and operating on data such as names and addresses.  To create a string variable, enclose the characters in single quotes ( ’ ’).  For example, the string variable n is created as follows: >>n= ’Omar Ahmad’ n= Omar Ahmad  The following string, number, is not the same as the variable number created by typing number = 123. >>number = ’123’ number = 123 3 (continued …) C7-2 Spring 2012

4  -Strings are stored as row vectors in which each column represents a character.  For example: The variable n has 1 row and 10 column (each blank space occupies one column). Thus: >> size(n) ans= 1 10  It can be accessed as follows: 4 >>n(6) ans= A >>first_name=n(1:4) first_name= Omar >> full_name=[n(1:4),' S. ',n(6:10)] full_name = Omar S. Ahmad String C7-2 Spring 2012

5 Relational Operators Mathematical MATLABMeaning <<Less than ≤<=Less than or equal to >>greater than ≥>=greater than or equal to ===equal to ≠~=not equal to 5 C7-2 Spring 2012

6 Examples of Relational Operators 6 (continued …) C7-2 Spring 2012

7 Note: the difference between the result obtained by x (x~=0) and the result obtained by find(x). 7 Examples of Relational Operators (continued …) C7-2 Spring 2012

8 Suppose x=[6,3,9,11] y=[14,2,9,13] Use MATLAB to find the values and indices of the elements in x that are less than the corresponding elements in y. 8 Examples of Relational Operators C7-2 Spring 2012

9 9 OperatorNameDefinition ~NOT ~A returns an array the same dimension as A; the new array has ones where A is zero and zeros where A is non zero. &AND A & B returns an array the same dimension as A and B; the new array has ones where both A and B have nonzero elements and zeros where either A or B is zero |OR A|B returns an array the same dimension as A and B; the new array has ones where at least one element in A or B is nonzero and zeros where A B are both zero.. (continued …) Logical Operators C7-2 Spring 2012

10 10 OperatorNameDefinition && Short-Circuit AND Operator for scalar logical expressions A && B returns true if both A and B evaluate to true, and false if they do not. || Short-Circuit OR Operator for scalar logical expressions A || B returns true if either A or B or both evaluate to true and false if they do not. Logical Operators C7-2 Spring 2012

11 xy~xx|yx&y True(1) False(0)True(1) False(0) True(1)False(0) True(1) False(0) True(1)False(0) Truth table 11 xy~x~xx|yx&y 11011 10010 01110 00100 C7-2 Spring 2012

12 Order of precedence for operator types: 1.Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^) 2.Unary plus (+), unary minus (-), logical negation (~) 3.Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\) 4.Addition (+), subtraction (-) 5.Colon operator (:) 6.Less than ( ), greater than or equal to (>=), equal to (==), not equal to (~=) 7.Element-wise logical AND (&) 8.Element-wise logical OR (|) 9.Short-circuit logical AND (&&) 10.Short-circuit logical OR (||) 12 C7-2 Spring 2012

13 Example - 01 Assume the variables x=5,y=6,and z=8. Indicate if each of the following conditions is true or false: a)x==5 ||y>3 b)2~=y && z==4 13 →→ F →→ T C7-2 Spring 2012

14 14 x=[2, 4,0];y=[-2,0,5]; z=[1 1 1]; what are the results of the following expressions? a)z|x&y|~z b) x>=z&~y<z Example - 02 Ans= 1 1 1 Ans= 1 0 0 C7-2 Spring 2012

15 Construct a logical expression to represent each of the following conditions: a.score is greater than or equal to 80 but less than 90 b.N is between 0 and 7 c.M is between -3 and 4 but not equal to 0 15 Example - 03 80<=score && score<90 Or score>=80 && score <90 0<N && N<7 Or N>0 && N<7 -3<M && M<4&&M~=0 C7-2 Spring 2012

16 Logical Array Logical arrays can be created with :  The relational operators  Logical operators  Logical function 16 (continued …) C7-2 Spring 2012

17  Logical arrays may have only the values 1(true) and 0 (false)  It is not necessarily any array contains only 0s and 1s a logical array. For example: >>x=[-2:2] x= -2 -1 0 1 2 >>k=(abs(x)>1) k= 1 0 0 0 1 >>z=x(k) z= -2 2 k and w appear the same; but k is logical array and w is a numeric array. 17 >>w=[1,0,0,0,1]; >>v=x(w) ??? Subscript indices must either be real positive integers or logical. Logical Array C7-2 Spring 2012

18 Logical function  -Logical function returns an array that can be used for logical indexing and logical tests.  -So to correct the error in the previous example, you may type instead w=logical([1,0,0,0,1]) before typing v=x(w). 18 C7-2 Spring 2012

19 Accessing arrays using logical arrays  When a logical array is used to address another array, it extracts from that array the elements in the locations where the logical array has 1s.  For example: Extract the diagonal elements of any array using : 19 >>C=A([1,5,9]) C= 5 9 13 >>B=logical(eye(3)) >>C=A(B) C= 5 9 13 OR >>A=[5,6,7;8,9,10;11,12,13]; C7-2 Spring 2012

20 Logical Operators & and the find Function >>x = [5, -3, 0, 0, 8]; y = [2, 4, 0, 5, 7]; >>z = find(x&y) z= 125 Note : The find function returns the indices, and not the values. Note : The difference between the result obtained by y(x&y) and the result obtained by find(x&y). >>values = y(x&y) values = 247 >>how_many = length(values) how_many = 3 20 C7-2 Spring 2012


Download ppt "Relational and Logical Operators EE 201 1C7-2 Spring 2012."

Similar presentations


Ads by Google