MATLAB Logical Expressions

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Exponents, Parentheses, and the Order of Operations.
Lesson 1-5 Solving Inequalities September Objective:
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Linear Systems The definition of a linear equation given in Chapter 1 can be extended to more variables; any equation of the form for real numbers.
Multiplying, Dividing, and Simplifying Radicals
RATIONAL EXPONENTS Assignments Assignments Basic terminology
Spreadsheets Objective 6.02
2 Explain advanced spreadsheet concepts and functions Advanced Calculations 1 Sabbir Saleh_Lecture_17_Computer Application_BBA.
Adding and Subtracting Real Numbers
Intermediate Algebra Prerequisite Topics Review Quick review of basic algebra skills that you should have developed before taking this class 18 problems.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Copyright © 2010 Pearson Education, Inc. All rights reserved. 1.2 – Slide 1.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Evaluating a Variable Expression To evaluate a variable expression:
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.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Real numbers In algebra, we work with the set of real numbers, which we can model using a number line. Real numbers describe real-world quantities such.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Any questions on today’s homework? (Sections 1.6/1.7) Reminder: You should be doing this homework without using a calculator, because calculators can’t.
Operators.
“Operators” (i.e. “symbols”)
1 CS161 Introduction to Computer Science Topic #6.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Computer Science 210 Computer Organization
Operators and Expressions
Introduction to Programming for Mechanical Engineers (ME 319)
Sequence, Selection, Iteration The IF Statement
Computing Fundamentals
Assignment statement:
MATLAB: Structures and File I/O
Computers & Programming Languages
All the Operators 22-Nov-18.
Relational Operators Operator Meaning < Less than > Greater than
All the Operators 4-Dec-18.
Computer Science 210 Computer Organization
Logical Operations In Matlab.
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
Chapter 9 Basic Algebra © 2010 Pearson Education, Inc. All rights reserved.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
All the Operators 6-Apr-19.
Relational Operators.
All the Operators 13-Apr-19.
Spreadsheets Objective 6.02
Chapter 3: Selection Structures: Making Decisions
Algebra 1 09/21/16 EQ: How do I solve equations with variables on both sides? HW: Due Friday pg. 95 # 1-33 all Bring textbooks tomorrow Quiz on Friday.
Spreadsheets Objective 6.02
Lesson Objective: I will be able to …
The boolean type and boolean operators
Introduction to Python
Presentation transcript:

MATLAB Logical Expressions Topics: 1. Relational and Logical Operators Instructor notes: This lecture basically covers branching in Matlab by looking at if-end, if-else-end, and if-elseif-else-end conditional statements. For students who have not done any prior programming this will be a fairly large concept to become comfortable with since they need to get used not only to the idea of a program taking different paths, but also the syntax of setting up the conditions. Instructional objectives: Acquire basic concepts of programming including evaluation of conditions Acquire knowledge about relational and logical operators. Use conditional statements (if–end/if-else–end/if-elseif-else–end) in MATLAB MATLAB - 8

Examples >> 5 > 8 ans = This statement is FALSE, so ans = 0 This statement is FALSE, so ans = 0 >> 5 < 8 ans = 1 This statement is TRUE, so ans = 1 Instructor notes: Here some quick examples. In the first Matlab asks “Is 5 greater than 8?”. Since the answer is false, the “5>8” relationship evaluates to FALSE, which is 0. So, Matlab returns a 0 for this relationship. In the next example Matlab asks, “Is 5 less than 8?”. Since the answer is true, the “5<8” relationship evaluates to TRUE, which is 1. In the third example, the result of the comparison from example 2 is assigned to the variable res. As a result res has the value 1. >> q = 5 < 8 q = 1 Assigns the result of the conditional operation to the variable name 'q' MATLAB - 8

The Logical Operators In MatLab: 1 is TRUE 0 is FALSE Operator Name Example Meaning & AND A & B TRUE when both A and B are true | OR A | B TRUE if either A or B are true ~ NOT ~ A TRUE if A is false. In MatLab: 1 is TRUE 0 is FALSE Actually, any number is true, except for 0, which is false. Instructor notes: In addition to the relational operators, we also have logical operators that evaluate operands to determine if a specific combination of the operands is true or false. The ampersand (&) is the operator used to evaluate if A AND B are true. For the AND condition to be true, both operands in the AND evaluate to true. A vertical bar (|) is used to evaluate if A OR B are true. For the OR condition to be true, either one or both of the operands in the OR must evaluate to true. The tilde (~) is the NOT operator. It negates the state of the operand. So, for ~(operand) to be TRUE, the operand must be false, and for ~(operand) to be FALSE, the operand must be false. Because some students may have experience with C/C++, it might be a good idea to point out that the NOT operator is different in Matlab than it is in C/C++ where NOT is represented by a “!”. It’s important to point out that nonzero numbers evaluate to TRUE and zero evaluates to FALSE. This can be difficult to get a grasp of, that the number 8, for example, would evaluate to TRUE, which is 1. There will be examples of this on the next slide. MATLAB - 8

Examples of logical operations 3 and 7 are both TRUE. So (3 & 7) is evaluated as (1 & 1) which is TRUE (= 1) a = 1 >> b = 5 | 0 b = 1 Instructor notes: These are very simple examples of logical operators which show the students the concept. They can read more from the book. 5 is TRUE and 0 is FALSE So (5 OR 0) is TRUE (= 1) MATLAB - 8

Math, Relational and Logical Operators Order of Precedence Name Highest 1 ( ) Parentheses 2 ^ Exponent 3 ~ Negation (Logical "NOT") 4 * / Multiply, Divide 5 + – Add, Subtract 6 < > <= >= == ~= Relational Operators 7 & Logical "AND" Lowest 8 | Logical "OR" MATLAB - 8

Order of precedence example >> y = 6 < 10 + 10 == 20/2 >> y = (6 < 10) + (10 == 20/2) = 6 < 10 + 10 == 10 = 6 < 20 == 10 TRUE TRUE TRUE + = 1 = 1 Instructor Notes: This example shows them how logical operators and the order of precedence works. == 1 10 Solution : y = 2 Solution : y = MATLAB - 8

RELATIONAL OPERATORS >> 5 + 4 > 8 ans = 1 >> 5 + (4>8) 5 >> y = ~1 & 0 y = Addition is done first, so true (1) 4>8 is false, so add 0 ~ is performed first, then & Instructor notes: Here some quick examples. In the first Matlab asks “Is 5 greater than 8?”. Since the answer is false, the “5>8” relationship evaluates to FALSE, which is 0. So, Matlab returns a 0 for this relationship. In the next example Matlab asks, “Is 5 less than 10?”. Since the answer is true, the “5<10” relationship evaluates to TRUE, which is 1. The result of the comparison is then assigned to the variable a. As a result a has the value 1. In the third example, three different relationships are evaluated. The first, “6<10”, evaluates to 1. The second, “7>8”, evaluates to 0. The third, “5*3==60/4” which becomes “15==15” evaluates to 1. The results of each of these relationships is summed and then the sum stored in y. . MATLAB - 8

Arrays in Logical Expressions Use of arrays in logical expressions will produce an array of 1s and 0s using element-by-element comparisons T=[23 34 25 67 56]; >>T>32 ans = 0 1 0 1 1 U=[0 5 6 0 0]; >>T&U 0 1 1 0 0 >>~(T&U) 1 0 0 1 1 >>sum(T>32) ans = 3 >> find(T>32) 2 4 5 >>find(T>32 & T<60) 2 5 Instructor notes: In addition to the relational operators, we also have logical operators that evaluate operands to determine if a specific combination of the operands is true or false. The ampersand (&) is the operator used to evaluate if A AND B are true. For the AND condition to be true, both operands in the AND evaluate to true. A vertical bar (|) is used to evaluate if A OR B are true. For the OR condition to be true, either one or both of the operands in the OR must evaluate to true. The tilde (~) is the NOT operator. It negates the state of the operand. So, for ~(operand) to be TRUE, the operand must be false, and for ~(operand) to be FALSE, the operand must be false. Because some students may have experience with C/C++, it might be a good idea to point out that the NOT operator is different in Matlab than it is in C/C++ where NOT is represented by a “!”. It’s important to point out that nonzero numbers evaluate to TRUE and zero evaluates to FALSE. This can be difficult to get a grasp of, that the number 8, for example, would evaluate to TRUE, which is 1. There will be examples of this on the next slide. P. 8 MATLAB - 8

Tips for HW MAT-08 CH. 6 prob 1,3,and 6 Best to write out prob 1 & 3. Let’s look at Problem 2d to prepare for HW Keep Order of Precedence chart nearby! On page 178 Problem 6 – you’ll need to know sum( ) and find () functions.. Let’s look at sample prob. 6-1 on page 181. Very Similar…