Lecture 8 Logical Operations Logical functions Relational operators

  • Slides: 13
Download presentation
Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All

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

Relational Operators n Relational operators perform element-by- element comparisons between two arrays. They return

Relational Operators n 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 < > == ~ Less than Greater than Equal

Relational Operators for Logical Operations < > == ~ 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 n The logical data type is a another MATLAB data type;

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

Relational operations and round-off errors in numerical computations n The == and ~= commands

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

What happened? MATLAB computes sin(p) to within eps, i. e, it computes a number

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

Logical Operators n Logical, or Boolean, operators: a logical operand that produces a logical

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

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

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

Hierarchy Of Operations n Logical Operators are evaluated after arithmetic and relational operations. Order

Hierarchy Of Operations n 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. n For example:

Logical Functions Logical functions are MATLAB functions that implement logical operations. n For example: n 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) row = 1 col= 6

Other Logical Functions n n n ischar() returns 1 if () contains character data

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

Exercises n Write a function that takes anything as input and prints to the

Exercises n 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 Relational Operators: < Less than > Greater than == Equal

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