Lecture 8 Logical Operations Logical functions Relational operators
- Slides: 13
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 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 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; 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 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 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 result. Operator & Operation Logical AND | Logical OR ~ Logical NOT
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 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: 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 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 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 to n Logical Operators: & And | Or ~ Not n Logical Functions: ischar(x) isinf(x) isnan(x) isnumeric(x) isempty(x) n n
- Relational operators
- Matlab relational operators
- Relational operators java
- Regular definition for unsigned numbers
- Relational operators in r
- Transition diagram for relational operators
- The limited tuple relational calculus equals:
- Relational calculus
- Relational algebra and relational calculus
- Object relational and extended relational databases
- Relational query language
- Matlab logical operators
- True false table
- Logical operators quiz