Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Similar presentations


Presentation on theme: "Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier."— Presentation transcript:

1 Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

2 Relational Operators Relational operators perform element-by- element comparisons between two arrays. They return a logical array of the same size, with elements set to logical 1 (true) where the relation is true, and elements set to logical 0 (false) where it is not. Relational operators perform element-by- element comparisons between two arrays. They return a logical array of the same size, with elements set to logical 1 (true) where the relation is true, and elements set to logical 0 (false) where it is not.

3 Relational Operators for Logical Operations < Lessthan < Less than > Greater than == Equal to (logical equality) ~ Not <= Less than or equal to ( ≤ ) >= Greater than or equal to ( ≥ ) ~= Not Equal to ( ≠ )

4 Logical Data Type inputOutput: ans =Data type 4 < 51Logical array 4 <= 51Logical array 4 > 50Logical array 4 >= 50Logical array 4 ~= 51Logical array The logical data type is a another MATLAB data type; it is either equal to 1 (true) or equal to 0 (false). The logical data type is a another MATLAB data type; it is either equal to 1 (true) or equal to 0 (false).

5 Relational operations and round-off errors in numerical computations The == and ~= commands can produce puzzling results due to round-off error. The == and ~= commands can produce puzzling results due to round-off error. For example: For example:

6 What happened? MATLAB computes sin(  )  to within eps, i.e, it computes a number approximately zero. MATLAB computes sin(  )  to within eps, i.e, it computes a number approximately zero. The == operator properly concludes that 0 and the computed value of sin(  ) are different. The == operator properly concludes that 0 and the computed value of sin(  ) are different.

7 Logical Operators Logical, or Boolean, operators: a logical operand that produces a logical result. Logical, or Boolean, operators: a logical operand that produces a logical result. OperatorOperation & Logical AND | Logical OR ~ Logical NOT

8 Logical Operators: Examples 0 (False) 0 1 (True) 11110

9 Hierarchy Of Operations Logical Operators are evaluated after Logical Operators are evaluated after arithmetic and relational operations. Order of Operations 1.Arithmetic 2.Relational Operators 3.All ~ (Not) operators 4.All & (And) operators evaluated from left to right 5.All | (Or) operators evaluated from left to right

10 Logical Functions Logical functions are MATLAB functions that implement logical operations. Logical functions are MATLAB functions that implement logical operations. For example: For example: row = 1col=6 The find() function returns the index numbers of the matrix that meet the logical statement given. Type the following into MATLAB: v = [1,3,5,6,3,4;6,36,6,7,843,5]; [ [ row,col ] = find(v == 4)

11 Other Logical Functions ischar() returns 1 if () contains character data ischar() returns 1 if () contains character data isinf() returns 1 if () contains infinity ( Inf ) isinf() returns 1 if () contains infinity ( Inf ) isnan() returns 1 if () contains not a number ( NaN ) isnan() returns 1 if () contains not a number ( NaN ) isnumeric() returns 1 if () contains numeric data isnumeric() returns 1 if () contains numeric data isempty() returns 1 if () contains empty ( x = [ ] ) isempty() returns 1 if () contains empty ( x = [ ] )

12 Exercises Write a function that takes anything as input and prints to the command window whether the input is a: Write a function that takes anything as input and prints to the command window whether the input is a: –Character –Infinite number –Not a number –Numeric –Empty A proper output would look like: logic_analysis(inf) Is Character: 0 Is Infinite: 1 Is Not a Number: 0 Is Numeric: 1 Is Empty: 0

13 Summary Logical Data Types Logical Data Types Relational Operators: Relational Operators: < Less than > Greater than == Equal to Logical Operators: Logical Operators: & And | Or ~ Not Logical Functions: Logical Functions: ischar(x) isinf(x) isnan(x) isnumeric(x) isempty(x)


Download ppt "Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier."

Similar presentations


Ads by Google