Relational and Logical Operators

Similar documents
Relational and Logical Statements

Chapter 7: Programming in MATLAB

Files and File Management Scripts Logical Operations Conditional Statements

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays

Dr. Iyad Jafar. Adapted from the publisher slides

CH6: Programming in MATLAB

Introduction to MATLAB for Engineers, Third Edition

Relational & Logical Operators

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

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

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

Introduction to Engineering gii

Informatics Ingeniería en Electrónica y Automática Industrial

Introduction to MATLAB

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

Algebra 1 Review. Properties of Real Numbers. Algebraic Expressions

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

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

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

RELATIONAL AND LOGICAL OPERATORS

Computer Programming CS F111

Department of Computer Science

Data Types and Variables in C language

GC03 Boolean Algebra

An Introduction to MATLAB

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

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

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

Boolean Logic & Branching Lab Conditional Tests

Only to be used for arranged hours. Order of Operations

Lecture 3 Programming in MATLAB. Dr. Bedir Yousif

Chapter 1 Introduction to MATLAB

Functions of Two Variables

Constants and Variables

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

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

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

Matlab and Psychophysics Toolbox Seminar Part 1. Introduction to Matlab

Digital Image Analysis and Processing CPE

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

OUTLINE. Number system. Creating MATLAB variables Overwriting variable Error messages Making corrections Entering multiple statements per line

The Distributive Property and Expressions Understand how to use the Distributive Property to Clear Parenthesis

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Working with Algebraic Expressions

NatSciLab - Numerical Software Introduction to MATLAB

Arithmetic operations

Prepared by: Shraddha Modi

Chap 6 - Introduction to HDL (b)

1 Overview of the standard Matlab syntax

Chapter 2. MATLAB Basis

Prof. Navrati Saxena TA: Rochak Sachan

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Structure Array 1 / 50

5. Using the precendence rules for arithmetic operators, determine which statement is true.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Flow Control. So Far: Writing simple statements that get executed one after another.

JAVA OPERATORS GENERAL

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Introduzione a MatLab. Prof. Sebastiano Battiato

Mathematical Operations with Arrays and Matrices

!"!!!"!!"!! = 10!!!!!(!!) = 10! = 1,000,000

Information Science 1

Lab 7 1 Due Thu., 6 Apr. 2017

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Project 2: How Parentheses and the Order of Operations Impose Structure on Expressions

1.8 Intro to Variables, Algebraic Expressions, and Equations

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

1 Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Operations and Properties

Introduction to MATLAB 7 for Engineers

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

UNIT 3 OPERATORS. [Marks- 12]

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

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

Introduction to Matlab

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Operators. Java operators are classified into three categories:

Variables and Assignments

Lecture 3 MATLAB programming (1) Dr.Qi Ying

Introduction to MATLAB

1 Introduction to MATLAB

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Computational Mathematics

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

MATLAB & Practical Applications on Climate Variability Studies tutorial

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block

Lecturer: Keyvan Dehmamy

Stokes Modelling Workshop

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

Fritz Keinert Department of Mathematics Iowa State University Ames, IA February 20, 2004

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Selection Statements

1 Introduction to Matlab

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

This appendix provides a quick start introduction to MATLAB. You should be

Course PJL. Arithmetic Operations

Transcription:

Relational and Logical Operators

Relational Operators Relational operators are used to represent conditions (such as space 0 in the water tank example) Result of the condition is either true or false In MATLAB: false is represented by 0 true is represented by 1 (non-zero) 2

Relational Operators Operator < > <= >= == ~= Description Less than Greater than Less than or equal to Greater than or equal to Equal to Not equal to 3

Relational Operators Operation Result 3 < 4 1 3 <= 4 1 3 == 4 0 3 ~= 4 1 3 > 4 0 4 >= 4 1 A < B 1 4

Relational Operators Don t confuse equivalance (==) with assignment (=) Relational operations have lower priority than arithmetic operations (use parentheses to be safe, though) Relational operators are used as arithmetic operators within a mathematical expression >>y=(6<10)+(7>8)+(5*3==60/4) y= 2» 1 + 0 + 1 5

Relational Operators Suppose that x = [6,3,9] and y = [14,2,9]. The following MATLAB session shows some examples. >>z = (x < y) z = 1 0 0 >>z = (x ~= y) z = 1 1 0 >>z = (x > 8) z = 0 0 1 6

Relational Operators The relational operators can be used for array addressing. 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. 7

Relational Operators >>5>8 ans= 0 >>a=5<10 a= 1 >>b=[15 6 9 4 11 7 14] >>c=[8 20 9 2 19 7 10] >>d=c>=b Checks which c elements are larger or equal to b elements d= 0 1 1 0 1 1 0 >>b==c Checks which b elements are ans= equal to c elements 0 0 1 0 0 1 0 >>b~=c Checks which b elements are ans= not equal to c elements 1 1 0 1 1 0 1 >>f= b-c>0 f= 1 0 0 1 0 0 1 Subtracts c from b and then checks which elements are larger than zero 8

Relational Operators >>a=[2 9 4;-3 5 2;6 7 1] A= 2 9 4-3 5 2 6 7-1 >>B=A<=2 Checks which A elements are smaller or equal to 2. Assigns results to B B= 1 0 0 1 0 1 0 0 1 >>3+4<16/2 + and / are executed first ans= 1 >>3+(4<16)/2 ans=? 9

Logical Operators Logical variables may have only the values 1 (true) and 0 (false). Logical operators: & AND OR xor ~ NOT Exclusive OR 10

Logical Operators input and or xor not 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 11

Logical Operators >>3&7 ans= 3 and 7 are both true 1 (nonzero) so outcome is 1 >>x=[9 3 0 11 0 15]; >>y=[2 0 13 11 0 4]; >>~25 ans= 25 is nonzero that is true 0 and opposite is false (0) >>x&y ans= 1 0 0 1 0 1 >>~(x+y) ans= 0 0 0 1 1 0 >>z=x y ans= 1 1 1 1 0 1 >>t=25*((12&0)+(~0)+(0 5)) t= 50 Use of logical operators in math expression 12

Operator Hierarchy Processing order of operations: parenthesis (starting from the innermost) exponentials (left to right) ~ operator multiplications and divisions (left to right) additions and subtractions (left to right) relational operators (left to right) & operator (left to right) operator (left to right) 13

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

Accessing Arrays Using Logical Arrays Just because an array contains only 0s and 1s, however, it is not necessarily a logical array. For example, in the following session k and w appear the same, but k is a logical array and w is a numeric array, and thus an error message is issued. >>x = [-2:2]; k = (abs(x)>1) k = 1 0 0 0 1 >>z = x(k) z = -2 2 >>w = [1,0,0,0,1]; v = x(w)??? Subscript indices must either be real positive integers or logicals. 15

Built-in Logical Functions MATLAB built-in logical functions are equivalent to logical operators and(a,b) equivalent to A&B or(a,b) equivalent to A B not(a) equivalent to ~A xor(a,b) exclusive OR,returns true if one operand is true and the other is false 16

Built-in Logical Functions isempty(x) returns 1 if X is an empty array and 0 otherwise isnumeric() returns 1 if X is numeric and 0 otherwise ischar() returns 1 if X is char and 0 otherwise isinf() returns 1 if given value equals inf isnan() returns 1 if given value equals NaN 17

Built-in Logical Functions all(x) returns a scalar, which is 1 if all the elements in the vector are nonzero and 0 otherwise. all(a) returns a row vector having the same number of columns as the matrix A and containing ones and zeros, depending on whether or not the corresponding column of A has all nonzero elements. any(x) returns a scalar, which is 1 if any of the elements in the vector x is nonzero and 0 otherwise. any(a) returns a row vector having the same number of columns as A and containing ones and zeros, depending on whether or not the corresponding column of the matrix A contains any nonzero elements. 18

The find Function find(a) [u,v,w] = find(a) x=[u v] Computes an array containing the indices of the nonzero elements of the array A in the form of linear indices. Computes the vectors u and v containing the row and column indices of the nonzero elements of the array A and computes the array w containing the values of the nonzero elements. The array w may be omitted.define x as concatenation of u and v to see row and column values together 19

Logical Operators and the find Function Consider the session >>x = [5, -3, 0, 0, 8];y = [2, 4, 0, 5, 7]; >>z = find(x&y) z = 1 2 5 Note that the find function returns the indices, and not the values. 20

Logical Operators and the find Function Remember, the find function returns the indices, and not the values. In the following session, note the difference between the result obtained by y(x&y) and the result obtained by find(x&y) in the previous slide. >>x = [5, -3, 0, 0, 8]; >>y = [2, 4, 0, 5, 7]; >>values = y(x&y) values = 2 4 7 >>how_many = length(values) how_many = 3 21