<<

ENGR 1181 | MATLAB 06: Logical Expressions Preparation Material

Learning Objectives 1. Apply logical operations correctly in arrays 2. Analyze data using logical operators

Topics Students will read Chapter 6.1 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading.

Students will learn a basic understanding of a relational operators and logical operators. This knowledge will be used to expand on the order of operations previously taught. Students will learn how to program arrays in logical expressions in MATLAB. This material contains the following:

1. Key Definitions 2. Relational Operators 3. Logical Operators 4. Order of Operations Expanded 5. Arrays in Logical Expressions

1. Key Definitions – compares two numbers by determining whether a comparison statement is true or false. Operators include: ‘less than <’, ‘greater than >’, ‘less than or equal to <=’, ‘greater than or equal to >=’, ‘equal to ==’, ‘not equal to ~=’.

Logical Operator – examines true/false statements and produces a result that is true (1) or false (0) according to the specific operator. Operators include ‘AND’ (&), ‘OR’ (|), ‘NOT’ (~).

Order of Precedence – computers and calculators follow a hierarchy for the order in which operations are executed. Higher-precedence operations are executed before lower-precedence operations; when they are equal, they are executed from left to right.

1 ENGR 1181 MATLAB 06: Logical Expressions Preparation Material

2. Relational Operators Relational operators compare two numbers in a comparison statement.

 If the statement is TRUE, it is assigned a value of 1.  If the statement is FALSE, it is assigned a value of 0.

Operator Meaning

< Less than. <= Less than or equal to. > Greater than. >= Greater than or equal to. == Equal to. ~= Not equal to.

5 is not larger than 8, so the answer is 0

5 is smaller than 10, so the answer is 1 and the answer (1) is stored in a

Refer to the MATLAB book for more examples of relational operators.

2 ENGR 1181 MATLAB 06: Logical Expressions Preparation Material

3. Logical Operators There are 3 major rules to logical operators:

 Logical operators have numbers as .  A nonzero number is TRUE.  A zero number is FALSE.

Logical Operator Name Meaning

& AND TRUE if both operands Example: A & B (A and B) are true.

| OR TRUE if either or both Example: A | B operands (A and B) are true.

~ NOT TRUE if the (A) is false. Example: ~ A FALSE if the operand (A) is true.

>> 3 & 7 ans = 3, 7 are both TRUE (nonzero) 1 So (3 AND 7) is TRUE (1)

>> a = 5 | 0 a = 5 or 0 1 5 is TRUE, so the answer is 1

>> x = -2; -5 < x < -1 is mathematically correct, but MATLAB returns FALSE (0). MATLAB >> -5 < x < -1 executes from left to right so the statement ans = is equivalent to (-5 < x) < -1 0 -5 < 1 is true (1), then 1 < -1 is false (0) >> -5 < x & x < -1 Correct statement is obtained with the ans = logical and (&) operator. The inequalities 1 are executed first. Both are true so the answer in 1

3 ENGR 1181 MATLAB 06: Logical Expressions Preparation Material

4. Order of Operations Expanded The order of operations (precedence) is now expanded from previous math operations. The new logical operators are shown below in red.

Operator Meaning

1 Parentheses (inner first if nested) 2 3 Logical NOT (~) 4 , 5 , 6 Relational Operators ( >, <. >=, <=, ==, ~= ) 7 Logical AND (&, &&) 8 Logical OR (|, ||)

>> 5 + 4 > 8

ans = Addition is done first, so true (1) 1

>> 5 + (4>8)

ans = 4>8 is false, so add 0 to the 5 5

>> y = ~1 & 0

y = ~ is performed first, then & 0

Refer to the MATLAB book for more examples of order of operations.

4 ENGR 1181 MATLAB 06: Logical Expressions Preparation Material

5. Arrays in Logical Expressions The use of arrays in logical expressions will produce an array of 1s and 0s using element-by- element comparisons.

>> T=[23 34 25 67 56];

>> U=[0 5 6 0 0];

>>T>32 ans = 0 1 0 1 1

>>T&U ans = 0 1 1 0 0

>>~(T&U) ans = 1 0 0 1 1

>>sum(T>32) ans = 3

>> find(T>32) ans = 2 4 5

>>find(T>32 & T<60) ans = 2 5

5