Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB Logical Expressions

Similar presentations


Presentation on theme: "MATLAB Logical Expressions"— Presentation transcript:

1 MATLAB Logical Expressions
Topics: 1. Relational and Logical Operators Instructor notes: This lecture basically covers branching in Matlab by looking at if-end, if-else-end, and if-elseif-else-end conditional statements. For students who have not done any prior programming this will be a fairly large concept to become comfortable with since they need to get used not only to the idea of a program taking different paths, but also the syntax of setting up the conditions. Instructional objectives: Acquire basic concepts of programming including evaluation of conditions Acquire knowledge about relational and logical operators. Use conditional statements (if–end/if-else–end/if-elseif-else–end) in MATLAB MATLAB - 8

2 Examples >> 5 > 8 ans = This statement is FALSE, so ans = 0
This statement is FALSE, so ans = 0 >> 5 < 8 ans = 1 This statement is TRUE, so ans = 1 Instructor notes: Here some quick examples. In the first Matlab asks “Is 5 greater than 8?”. Since the answer is false, the “5>8” relationship evaluates to FALSE, which is 0. So, Matlab returns a 0 for this relationship. In the next example Matlab asks, “Is 5 less than 8?”. Since the answer is true, the “5<8” relationship evaluates to TRUE, which is 1. In the third example, the result of the comparison from example 2 is assigned to the variable res. As a result res has the value 1. >> q = 5 < 8 q = 1 Assigns the result of the conditional operation to the variable name 'q' MATLAB - 8

3 The Logical Operators In MatLab: 1 is TRUE 0 is FALSE
Operator Name Example Meaning & AND A & B TRUE when both A and B are true | OR A | B TRUE if either A or B are true ~ NOT ~ A TRUE if A is false. In MatLab: 1 is TRUE 0 is FALSE Actually, any number is true, except for 0, which is false. Instructor notes: In addition to the relational operators, we also have logical operators that evaluate operands to determine if a specific combination of the operands is true or false. The ampersand (&) is the operator used to evaluate if A AND B are true. For the AND condition to be true, both operands in the AND evaluate to true. A vertical bar (|) is used to evaluate if A OR B are true. For the OR condition to be true, either one or both of the operands in the OR must evaluate to true. The tilde (~) is the NOT operator. It negates the state of the operand. So, for ~(operand) to be TRUE, the operand must be false, and for ~(operand) to be FALSE, the operand must be false. Because some students may have experience with C/C++, it might be a good idea to point out that the NOT operator is different in Matlab than it is in C/C++ where NOT is represented by a “!”. It’s important to point out that nonzero numbers evaluate to TRUE and zero evaluates to FALSE. This can be difficult to get a grasp of, that the number 8, for example, would evaluate to TRUE, which is 1. There will be examples of this on the next slide. MATLAB - 8

4 Examples of logical operations
3 and 7 are both TRUE. So (3 & 7) is evaluated as (1 & 1) which is TRUE (= 1) a = 1 >> b = 5 | 0 b = 1 Instructor notes: These are very simple examples of logical operators which show the students the concept. They can read more from the book. 5 is TRUE and 0 is FALSE So (5 OR 0) is TRUE (= 1) MATLAB - 8

5 Math, Relational and Logical Operators Order of Precedence
Name Highest 1 ( ) Parentheses 2 ^ Exponent 3 ~ Negation (Logical "NOT") 4 * / Multiply, Divide 5 + – Add, Subtract 6 < > <= >= == ~= Relational Operators 7 & Logical "AND" Lowest 8 | Logical "OR" MATLAB - 8

6 Order of precedence example
>> y = 6 < == 20/2 >> y = (6 < 10) + (10 == 20/2) = 6 < == 10 = 6 < 20 == 10 TRUE TRUE TRUE + = 1 = 1 Instructor Notes: This example shows them how logical operators and the order of precedence works. == 1 10 Solution : y = 2 Solution : y = MATLAB - 8

7 RELATIONAL OPERATORS >> 5 + 4 > 8 ans = 1
>> 5 + (4>8) 5 >> y = ~1 & 0 y = Addition is done first, so true (1) 4>8 is false, so add 0 ~ is performed first, then & Instructor notes: Here some quick examples. In the first Matlab asks “Is 5 greater than 8?”. Since the answer is false, the “5>8” relationship evaluates to FALSE, which is 0. So, Matlab returns a 0 for this relationship. In the next example Matlab asks, “Is 5 less than 10?”. Since the answer is true, the “5<10” relationship evaluates to TRUE, which is 1. The result of the comparison is then assigned to the variable a. As a result a has the value 1. In the third example, three different relationships are evaluated. The first, “6<10”, evaluates to 1. The second, “7>8”, evaluates to 0. The third, “5*3==60/4” which becomes “15==15” evaluates to 1. The results of each of these relationships is summed and then the sum stored in y. . MATLAB - 8

8 Arrays in Logical Expressions
Use of arrays in logical expressions will produce an array of 1s and 0s using element-by-element comparisons T=[ ]; >>T>32 ans = U=[ ]; >>T&U >>~(T&U) >>sum(T>32) ans = 3 >> find(T>32) >>find(T>32 & T<60) 2 5 Instructor notes: In addition to the relational operators, we also have logical operators that evaluate operands to determine if a specific combination of the operands is true or false. The ampersand (&) is the operator used to evaluate if A AND B are true. For the AND condition to be true, both operands in the AND evaluate to true. A vertical bar (|) is used to evaluate if A OR B are true. For the OR condition to be true, either one or both of the operands in the OR must evaluate to true. The tilde (~) is the NOT operator. It negates the state of the operand. So, for ~(operand) to be TRUE, the operand must be false, and for ~(operand) to be FALSE, the operand must be false. Because some students may have experience with C/C++, it might be a good idea to point out that the NOT operator is different in Matlab than it is in C/C++ where NOT is represented by a “!”. It’s important to point out that nonzero numbers evaluate to TRUE and zero evaluates to FALSE. This can be difficult to get a grasp of, that the number 8, for example, would evaluate to TRUE, which is 1. There will be examples of this on the next slide. P. 8 MATLAB - 8

9 Tips for HW MAT-08 CH. 6 prob 1,3,and 6
Best to write out prob 1 & 3. Let’s look at Problem 2d to prepare for HW Keep Order of Precedence chart nearby! On page 178 Problem 6 – you’ll need to know sum( ) and find () functions.. Let’s look at sample prob. 6-1 on page Very Similar…


Download ppt "MATLAB Logical Expressions"

Similar presentations


Ads by Google