ENGR 1181 MATLAB 06: Logical Expressions
Preparation Material

ENGR 1181 | MATLAB 06: Logical ExpressionsPreparation 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

Relational Operator – 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.

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

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

3. Logical Operators

There are 3 major rules to logical operators:

·  Logical operators have numbers as operands.

·  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 operand (A) is false.

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

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 Exponentiation

3 Logical NOT (~)

4 Multiplication, Division

5 Addition, Subtraction

6 Relational Operators

( >, <. >=, <=, ==, ~= )

7 Logical AND (&, &)

8 Logical OR (|, ||)

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

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.

2