Relational and Logical Statements

Similar documents
Chapter 7: Programming in MATLAB

Relational and Logical Operators

Relational & Logical Operators

CH6: Programming in MATLAB

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

C/C++ Programming for Engineers: Matlab Branches and Loops

Files and File Management Scripts Logical Operations Conditional Statements

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Chap 6 - Introduction to HDL (b)

Lecture 3 Programming in MATLAB. Dr. Bedir Yousif

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Boolean Logic & Branching Lab Conditional Tests

SMS 3515: Scientific Computing. Sem /2015

Arithmetic operations

RELATIONAL AND LOGICAL OPERATORS

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

ECEN 468 Advanced Logic Design

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

GC03 Boolean Algebra

Unit 3. Operators. School of Science and Technology INTRODUCTION

Department of Computer Science

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Topics. Chapter 5. Equality Operators

Operations and Properties

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 3: Operators, Expressions and Type Conversion

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Unit 2: Accentuate the Negative Name:

Operators & Expressions

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

Dr Richard Greenaway

Conditional Statement

ENGR 105: Introduction to Scientific Computing. Dr. Graham. E. Wabiszewski

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Chapter 4: Basic C Operators

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER. Chapter 2

function [s p] = sumprod (f, g)

Dr. Khaled Al-Qawasmi

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

Computer Programming CS F111

Arithmetic and Bitwise Operations on Binary Data

Matlab and Psychophysics Toolbox Seminar Part 1. Introduction to Matlab

Advanced Algebra I Simplifying Expressions

Arithmetic and Logic Blocks

7 Control Structures, Logical Statements

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Compact Matlab Course

JAVA OPERATORS GENERAL

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Unit 3: Multiplication and Division Reference Guide pages x 7 = 392 factors: 56, 7 product 392

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Operators. Java operators are classified into three categories:

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Chapter 4 Section 2 Operations on Decimals

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

Full file at C How to Program, 6/e Multiple Choice Test Bank

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

McTutorial: A MATLAB Tutorial

Lecture 3 Tao Wang 1

Lesson 38: Conditionals #2 (W11D3)

Introduction to Engineering gii

Operators And Expressions

Programming Logic and Design Sixth Edition

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Summer Packet 7 th into 8 th grade. Name. Integer Operations = 2. (-7)(6)(-4) = = = = 6.

EGR 111 Introduction to MATLAB

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

UNIT- 3 Introduction to C++

1 Week 1: Basics of scientific programming I

Integer Operations. Summer Packet 7 th into 8 th grade 1. Name = = = = = 6.

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Today s topics. Announcements/Reminders: Characters and strings Review of topics for Test 1

Introduction to MATLAB for Engineers, Third Edition

Chapter 2, Part III Arithmetic Operators and Decision Making

Using Basic Formulas 4

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Operators in java Operator operands.

SMS 3515: Scientific Computing Lecture 1: Introduction to Matlab 2014

Lecture 2: Variables, Vectors and Matrices in MATLAB

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

ADDING AND SUBTRACTING RATIONAL EXPRESSIONS

LAB 2 VECTORS AND MATRICES

One-dimensional Array

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Formulas and Functions

Expression and Operator

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

Review for Exam 2. IF Blocks. Nested IF Blocks. School of Business Eastern Illinois University. Represent computers abilities to make decisions

Introduction to MATLAB

ECE Lesson Plan - Class #3 Script M-files, Program Control, and Complex Numbers

Transcription:

Relational and Logical Statements

Relational Operators in MATLAB A operator B A and B can be: Variables or constants or expressions to compute Scalars or arrays Numeric or string Operators: > (greater than) > = (greater than or equal to) < (less than) < = (less than or equal to) = = (equal to) ~ = (not equal to)

Ultimately we ll be using relational expressions in an IF statement Examples Absolute value: if a<0 a = -a end Compute letter grade: if gpa >= 90 letter_grade = A end

Logical variables The Result of comparing with a relational operator is a logical scalar or vector: If "true", MATLAB gives the comparison a value of one (1) If "false", MATLAB gives the comparison a value of zero (0)

Logical Operators Logical Operators in MATLAB are: Logical Operator & (AND) Ex. A&B Description If both operands (A and B) are true, the result is true, otherwise the result is false. (OR) A B If either operand (A and B) is true, the result is true, otherwise (both are false) the result is false. ~ (NOT) ~A Operates on 1 operand (A). Gives the opposite of the operand. True (1) if the operand is false, and false (0) if the operand is true.

Examples: Are you between 25 and 30 years old? (age>=25) & (age<=30) Is it winter? (month==12 & day>=22) (month==1) (month==2) (month==3 & day<=21)

Don t confuse == and = a == b is a relational comparison which evaluates to True or False a = b is an assignment statement, assigning the value of b to a

Round-off errors may affect == and ~=.33333 ==.33333 1 (True).33333 == 1/3 0 (False) instead, test that they are nearly equal, within some tolerance: abs(.33333-1/3) < =.0001 1

You can compare a vector with a scalar The result is a logical vector, the same size as the one being compared It helps to picture in your mind the result of a logical comparison >> x=8:12 x = 8 9 10 11 12 >> x>10 ans = 0 0 0 1 1

Example >> x=8:12 x = 8 9 10 11 12 >> x>10 ans = 0 0 0 1 1 >> x==11 ans = 0 0 0 1 0 >> x>=7 ans = 1 1 1 1 1

Compare 2 vectors They must be the same size >> A = [6 4 10]; >> B = [0 4 5]; >> A==B ans = 0 1 0

Strings are treated just like vectors >> A = Tom >> B = m % m is a scalar >> A == B ans = 0 0 1 >> Tom == Bob ans = 0 1 0

More Examples: expression 5 < 7 [ 3, 5, 2 ] > = [ 1, 0, 12 ] max( 1:6 ) < = 7 [3, pi, -12 ] > 1 'Tom' == 'Bob' 'Tom' == 'm' result 1 1 1 0 1 1 1 0 0 1 0 0 0 1

When comparing arrays They must be the same dimensions MATLAB does an element-wise comparison Result is an array that has same dimensions as other two but only contains 1's and 0's

When comparing array to scalar MATLAB compares scalar to every member of array Result is an array that has same dimensions as original but only contains 1's and 0's

Accessing Arrays Using Logical Arrays When a logical array is used to address another array, it extracts from that array the elements in the locations where the logical array has 1s. So typing A(B), where B is a logical array of the same size as A, returns the values of A at the indices where B is 1.

Example returning a scalar For example, with x = [6,3,9] and y = [14,2,9], typing z = x(x<y) finds all the elements in x that are less than the corresponding elements in y. The result is z = 6. result of x<y is [1 0 0] returns the value of x at the indices where x<y is 1

**Example returning a vector >> r = [ 8 12 9 4 23 19 10]; Define a vector r >> s = r<=10 s = 1 0 1 1 0 0 1 Checks which r elements are smaller than or equal to 10 Logical vector s with 1 s at positions where elements of r are smaller than or equal to 10 >> t=r(s) t = 8 9 4 10 Use s to select elements from vector r to create vector t Vector t consists of elements of r in positions where s has 1 s

Same result can be achieved in one step >> r = [ 8 12 9 4 23 19 10] % Extract the elements of r that are % less than or equal to 10: >> w=r(r<=10) w = 8 9 4 10

Just because an array contains only 0s and 1s, however, does not make it a logical array. >> x = [-2:2]; -2-1 0 1 2 >> i_logical = x>0; 0 0 0 1 1 >> i_double = [0 0 0 1 1]; 0 0 0 1 1 >> x(i_logical); 1 2 >> x(i_double) Subscript indices must either be real positive integers or logicals.

Array example: Score = [ 70, 55, 88, 98, 80, 73, 90] C = (Score > 70) & (Score < 81) C = [ 0 0 0 0 1 1 0 ] Useful in counting how many entries satisfy a condition: B_grades = sum( Score<91 & Score>80 )

Built-in Logical Functions MATLAB has built-in functions that are equivalent to the logical operators. and(a,b) or(a,b) equivalent to A&B equivalent to A B not(a) equivalent to ~A

Other Logical Built-in Functions Function Description Example xor(a,b) all(a) any(a) Exclusive or. Returns true (1) if one operand is true and the other is false. Returns 1 (true) if all elements in a vector A are true (nonzero). If A is a matrix, treats columns of A as vectors, returns a vector with 1 s and 0 s. Returns 1 (true) if any element in a vector A is true (nonzero). If A is a matrix, treats columns of A as vectors, returns a vector with xor(7,0) ans = 1 xor(7, -5) ans = 0 A=[6 2 15 9 7 11]; all(a) ans = 1 B=[6 2 15 9 0 11]; all(b) ans = 0 A=[6 0 15 0 0 11]; any(a) ans = 1 B=[0 0 0 0 0 0];

Other Logical Built-in Functions Function Description Example find(a) find (A>d) logical(a) If A is a vector, returns the indices of the nonzero elements. If A is a vector, returns the address of the elements that are larger than d (any relational operator can be used) Converts the elements of the array A into logical values. A=[0 9 4 3 7 0 0 1 8]; find(a) ans = 2 3 4 5 8 9 find(a>4) ans= 2 5 9

Basic Logical Operators: and & ; or ; xor xor ; not ~ Truth Table A B A&B A B xor(a,b) ~A 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 Note: xor (Exclusive Or) Returns true (1) if one operand is true and the other is false.

Operator Precedence (highest to lowest) 1. Parentheses ( ). If nested parentheses exist, inner have precedence. 2. Transpose(') and power(.^) 3. Negation (-) and logical negation (~) 4. Multiplication (.*) and division (./), 5. Addition (+) and subtraction (-) 6. Colon operator (:) 7. Relational operators (<, <=, >, >=, = =, ~=) 8. Logical AND (&) 9. Logical OR ( )

Practice logicals x = [1, 2, 3, 4, 5]; y = [-2, 0, 2, 4, 6] 1. what is the result of x < y 2. how many elements are not equal? 3. print the elements of y that are less than 0

Difference between logical expressions and Find Logical expressions return a logical scalar or array A = [1 5 7 8 0 0]; A > 5 returns [0 0 1 1 0 0] Find returns a list of indices find (A>5) returns [3, 4]

Difference between logical expressions and Find A = [1 5 7 8 0 0]; Set B equal to the non-zero elements of A 1. B = A(A~= 0) 2. B = A(find(A~=0)) Same answer. Method 2 is preferred for large sparse arrays (e.g. images)