Relational and Logical Operators EE 201 1C7-2 Spring 2012.

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

Introduction to arrays
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Decision-Making Programs
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
If Statements & Relational Operators Programming.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
Spreadsheets Objective 6.02
2 Explain advanced spreadsheet concepts and functions Advanced Calculations 1 Sabbir Saleh_Lecture_17_Computer Application_BBA.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Matlab tutorial course Lesson 2: Arrays and data types
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 4 Programming with MATLAB PowerPoint to accompany Copyright © The.
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
Chapter 8 Matrices and Determinants Matrix Solutions to Linear Systems.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
LESSON 3 MATRICES Azalya Rahmatika Fathul Fithrah JURUSAN MATEMATIKA FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM UNIVERSITAS SYIAH KUALA DARUSSALAM,
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Operators & Expressions
Unit 3 Matrix Arithmetic IT Disicipline ITD 1111 Discrete Mathematics & Statistics STDTLP 1 Unit 3 Matrix Arithmetic.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Array and Matrix Operations EE 201 Fall  Achieve Comprehension LOL of Array and Matrix Operations. Class Learning Objectives 2.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Introduction to MATLAB for Engineers, Third Edition
ECE 1304 Introduction to Electrical and Computer Engineering
Relational Operators Operator Meaning < Less than > Greater than
Vectorized Code, Logical Indexing
C Operators, Operands, Expressions & Statements
Introduction to MATLAB [Vectors and Matrices] Lab 2
INTRODUCTION TO MATLAB
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Spreadsheets Objective 6.02
Matlab Basics.
Spreadsheets Objective 6.02
Presentation transcript:

Relational and Logical Operators EE 201 1C7-2 Spring 2012

 Achieve Comprehension LOL of using Relational and Logical Operators in MATLAB. Class Learning Objectives 2 C7-2 Spring 2012

String  A string is a variable that contains characters.  Strings are useful for creating input prompts and messages and for storing and operating on data such as names and addresses.  To create a string variable, enclose the characters in single quotes ( ’ ’).  For example, the string variable n is created as follows: >>n= ’Omar Ahmad’ n= Omar Ahmad  The following string, number, is not the same as the variable number created by typing number = 123. >>number = ’123’ number = (continued …) C7-2 Spring 2012

 -Strings are stored as row vectors in which each column represents a character.  For example: The variable n has 1 row and 10 column (each blank space occupies one column). Thus: >> size(n) ans= 1 10  It can be accessed as follows: 4 >>n(6) ans= A >>first_name=n(1:4) first_name= Omar >> full_name=[n(1:4),' S. ',n(6:10)] full_name = Omar S. Ahmad String C7-2 Spring 2012

Relational Operators Mathematical MATLABMeaning <<Less than ≤<=Less than or equal to >>greater than ≥>=greater than or equal to ===equal to ≠~=not equal to 5 C7-2 Spring 2012

Examples of Relational Operators 6 (continued …) C7-2 Spring 2012

Note: the difference between the result obtained by x (x~=0) and the result obtained by find(x). 7 Examples of Relational Operators (continued …) C7-2 Spring 2012

Suppose x=[6,3,9,11] y=[14,2,9,13] Use MATLAB to find the values and indices of the elements in x that are less than the corresponding elements in y. 8 Examples of Relational Operators C7-2 Spring 2012

9 OperatorNameDefinition ~NOT ~A returns an array the same dimension as A; the new array has ones where A is zero and zeros where A is non zero. &AND A & B returns an array the same dimension as A and B; the new array has ones where both A and B have nonzero elements and zeros where either A or B is zero |OR A|B returns an array the same dimension as A and B; the new array has ones where at least one element in A or B is nonzero and zeros where A B are both zero.. (continued …) Logical Operators C7-2 Spring 2012

10 OperatorNameDefinition && Short-Circuit AND Operator for scalar logical expressions A && B returns true if both A and B evaluate to true, and false if they do not. || Short-Circuit OR Operator for scalar logical expressions A || B returns true if either A or B or both evaluate to true and false if they do not. Logical Operators C7-2 Spring 2012

xy~xx|yx&y True(1) False(0)True(1) False(0) True(1)False(0) True(1) False(0) True(1)False(0) Truth table 11 xy~x~xx|yx&y C7-2 Spring 2012

Order of precedence for operator types: 1.Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^) 2.Unary plus (+), unary minus (-), logical negation (~) 3.Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\) 4.Addition (+), subtraction (-) 5.Colon operator (:) 6.Less than ( ), greater than or equal to (>=), equal to (==), not equal to (~=) 7.Element-wise logical AND (&) 8.Element-wise logical OR (|) 9.Short-circuit logical AND (&&) 10.Short-circuit logical OR (||) 12 C7-2 Spring 2012

Example - 01 Assume the variables x=5,y=6,and z=8. Indicate if each of the following conditions is true or false: a)x==5 ||y>3 b)2~=y && z==4 13 →→ F →→ T C7-2 Spring 2012

14 x=[2, 4,0];y=[-2,0,5]; z=[1 1 1]; what are the results of the following expressions? a)z|x&y|~z b) x>=z&~y<z Example - 02 Ans= Ans= C7-2 Spring 2012

Construct a logical expression to represent each of the following conditions: a.score is greater than or equal to 80 but less than 90 b.N is between 0 and 7 c.M is between -3 and 4 but not equal to 0 15 Example <=score && score<90 Or score>=80 && score <90 0<N && N<7 Or N>0 && N<7 -3<M && M<4&&M~=0 C7-2 Spring 2012

Logical Array Logical arrays can be created with :  The relational operators  Logical operators  Logical function 16 (continued …) C7-2 Spring 2012

 Logical arrays may have only the values 1(true) and 0 (false)  It is not necessarily any array contains only 0s and 1s a logical array. For example: >>x=[-2:2] x= >>k=(abs(x)>1) k= >>z=x(k) z= -2 2 k and w appear the same; but k is logical array and w is a numeric array. 17 >>w=[1,0,0,0,1]; >>v=x(w) ??? Subscript indices must either be real positive integers or logical. Logical Array C7-2 Spring 2012

Logical function  -Logical function returns an array that can be used for logical indexing and logical tests.  -So to correct the error in the previous example, you may type instead w=logical([1,0,0,0,1]) before typing v=x(w). 18 C7-2 Spring 2012

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.  For example: Extract the diagonal elements of any array using : 19 >>C=A([1,5,9]) C= >>B=logical(eye(3)) >>C=A(B) C= OR >>A=[5,6,7;8,9,10;11,12,13]; C7-2 Spring 2012

Logical Operators & and the find Function >>x = [5, -3, 0, 0, 8]; y = [2, 4, 0, 5, 7]; >>z = find(x&y) z= 125 Note : The find function returns the indices, and not the values. Note : The difference between the result obtained by y(x&y) and the result obtained by find(x&y). >>values = y(x&y) values = 247 >>how_many = length(values) how_many = 3 20 C7-2 Spring 2012