Relational and Logical Operators EE 201 C 7

  • Slides: 20
Download presentation
Relational and Logical Operators EE 201 C 7 -2 Spring 2012 1

Relational and Logical Operators EE 201 C 7 -2 Spring 2012 1

Class Learning Objectives o Achieve Comprehension LOL of using Relational and Logical Operators in

Class Learning Objectives o Achieve Comprehension LOL of using Relational and Logical Operators in MATLAB. C 7 -2 Spring 2012 2

String ØA string is a variable that contains characters. ØStrings are useful for creating

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 (’ ’). >>n= ’Omar Ahmad’ ØFor example, the string n= variable n is created as follows: Omar Ahmad ØThe following string, number, is not the same as the variable number created by typing number = 123. >>number = ’ 123’ number = (continued …) 123 C 7 -2 Spring 2012 3

String Ø-Strings are stored as row vectors in which each column represents a character.

String Ø-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) >>n(6) ans= 1 10 A ØIt can be accessed as follows: >>first_name=n(1: 4) first_name= Omar >> full_name=[n(1: 4), ' S. ', n(6: 10)] full_name = Omar S. Ahmad C 7 -2 Spring 2012 4

Relational Operators Mathematical MATLAB < < Less than ≤ > <= > Less than

Relational Operators Mathematical MATLAB < < Less than ≤ > <= > Less than or equal to greater than ≥ >= greater than or equal to = == equal to ≠ ~= not equal to C 7 -2 Meaning Spring 2012 5

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

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

Examples of Relational Operators Note: the difference between the result obtained by x (x~=0)

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

Examples of Relational Operators Suppose x=[6, 3, 9, 11] y=[14, 2, 9, 13] Use

Examples of Relational Operators 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. C 7 -2 Spring 2012 8

. Logical Operators Operator ~ & | Name Definition NOT ~A returns an array

. Logical Operators Operator ~ & | Name Definition 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 …) C 7 -2 Spring 2012 9

. Logical Operators Operator && || Name Definition Short-Circuit AND Operator for scalar logical

. Logical Operators Operator && || Name Definition 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 C 7 -2 Spring 2012 10

Truth table x y ~x x|y x&y True(1) False(0) True(1) True(1) False(0) False(0) True(1)

Truth table x y ~x x|y x&y True(1) False(0) True(1) True(1) False(0) False(0) True(1) False(0) x y ~x x|y x&y 1 1 0 1 1 1 0 0 0 1 0 0 C 7 -2 Spring 2012 11

Order of precedence for operator types: 1. Transpose (. '), power (. ^), complex

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

Example - 01 Assume the variables x=5, y=6, and z=8. Indicate if each of

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 →→ T b) 2~=y && z==4 →→ C 7 -2 F Spring 2012 13

Example - 02 x=[2, 4, 0]; y=[-2, 0, 5]; z=[1 1 1]; what are

Example - 02 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 Ans= b) x>=z&~y<z 1 1 0 0 Ans= C 7 -2 Spring 2012 14

Example - 03 Construct a logical expression to represent each of the following conditions:

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

Logical Array Logical arrays can be created with : v. The relational operators v.

Logical Array Logical arrays can be created with : v. The relational operators v. Logical function (continued …) C 7 -2 Spring 2012 16

Logical Array ØLogical arrays may have only the values 1(true) and 0 (false) ØIt

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

Logical function Ø-Logical function returns an array that can be used for logical indexing

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). C 7 -2 Spring 2012 18

Accessing arrays using logical arrays ØWhen a logical array is used to address another

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 1 s. Ø For example: Extract the diagonal elements of any array using : >>A=[5, 6, 7; 8, 9, 10; 11, 12, 13]; >>B=logical(eye(3)) >>C=A(B) >>C=A([1, 5, 9]) OR C= 5 9 9 13 13 C 7 -2 Spring 2012 19

Logical Operators & and the find Function >>x = [5, -3, 0, 0, 8];

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