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

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.

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 ( ≠ )

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).

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:

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.

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

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

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

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)

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 = [ ] )

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

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)