GT AE 6382 - Relational and Logical Operators

Unformatted text preview:

Relational and Logical OperatorsRecall from Arithmetic OperationsRelational OperatorsOrder of PrecedenceLogical OperatorsLogical Operators: AND, OR, NOTExamples:Using Logical Values in AssignmentsMore Examples:Precedence Expanded: See Section 9.3PracticeMore practiceFall 2006AE6382 Design Computing 1Relational and Logical Operators•Use relational operators to test two values•Work with values of true and false•Compare relationships using logical operators•Analyze the precedence of operatorsFall 2006AE6382 Design Computing 2Recall from Arithmetic Operations•Arithmetic operators:–+, - , *, / , ^•Rules of precedence for arithmetic operators:–1. Exponents–2. Multiplication / Division–3. Addition/Subtraction•We can have a combination of arithmetic operators for multiple values.Fall 2006AE6382 Design Computing 3Relational Operators•Used to compare two values•Returns a value of true or false. •In Matlab, –1 = true (actually non-zero);–0 = false;Relational Operators:< less than<= less than or equal to> greater than>= greater than or equal to== equals to~ = not equal toFall 2006AE6382 Design Computing 4Order of Precedence•When relational operators are present:–All arithmetic operations are performed first (in their particular order)–The relational operators are evaluated after.•Example 1(2*3) > (4+1); % solve by hand, then type into MATLAB-The multiplication and addition are first: -6 > 5-The relational operator is evaluated:-6 is greater than 5, so this returns 1 (true)Fall 2006AE6382 Design Computing 5Logical Operators•Logical Operators:–Evaluate the relationship between what relational operators return.–Return a value of true or false.•& AND•| OR•~ NOT•Evaluated after all other operators have been performed.Fall 2006AE6382 Design Computing 6Logical Operators: AND, OR, NOT•AND: &–Returns true if two expressions being compared are true.–Returns false if any of the two is false.•OR: |–Returns true if any of the two expressions is true.–Returns false only if the two are false.•NOT: ~–Returns true if the single expression is false.–Returns false if the single expression is true.Fall 2006AE6382 Design Computing 7Examples:•a=7; b=4; c=3;•~(a==3*b)–Evaluates 3*b = 12–Reads: is (a==12) not (from the ~) true?–Returns ans = 1 (true)•a > 5 & b > 5–Evaluates (a>5) and (b>5) separately. –One returns true, the other returns false.–Since both are not true, the expression returns false.Fall 2006AE6382 Design Computing 8Using Logical Values in Assignments•True/False values can be assigned to variables.•The variables will be assigned the value that returns from relational and/or logical operators.•The variables will have a value of 1 or 0.•Example:–X = a > 2;•Then x = 1;–Y = b==5;•Y will be equal to 0.•This kind of arithmetic is VERY common in Matlab!Fall 2006AE6382 Design Computing 9More Examples:•a=6; b=10; c=-2;•Try the following examples without the use of Matlab:–X1 = abs(c)>3 & c<(b-a) & b+a > 3–X2 = (b==10) | (a< 4)–X3 = a.*5 >= b.*3 & c < a•Safety tip: use parentheses to make expression more readable, see example X2.Fall 2006AE6382 Design Computing 10Precedence Expanded: See Section 9.31. 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 (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)7. logical AND (&)8. logical OR (|)Fall 2006AE6382 Design Computing 11Practice•Evaluate the following without Matlab:–Practice first without the help of Matlab, and then test your expected results using Matlab…–a = 4; b = 20; c = 12; d = 5;–One = a>4 & b==20–Two = b<40 | c>10–Three = d.*c > a.*b–Four = b.*3<= 100 & d<10 & a.*d==bFall 2006AE6382 Design Computing 12More practice•When comparing vectors, the operator (>, <=, ~, AND, etc.) is applied element-by-element:a = [0,2,4,2]; b = [4,1,-2,3];•What is:–C = a .* b;–C = b.^2-a.*b–C = a >=


View Full Document

GT AE 6382 - Relational and Logical Operators

Download Relational and Logical Operators
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Relational and Logical Operators and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Relational and Logical Operators 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?