Boolean Logic in MATLAB



Similar documents
Independent samples t-test. Dr. Tom Pierce Radford University

1.4 Compound Inequalities

Pre-Algebra - Order of Operations

MULTIPLICATION AND DIVISION OF REAL NUMBERS In this section we will complete the study of the four basic operations with real numbers.

Training Manual. Pre-Employment Math. Version 1.1

Linear Programming Problems

Inequalities - Solve and Graph Inequalities

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

2.6 Exponents and Order of Operations

7. Solving Linear Inequalities and Compound Inequalities

Inequalities - Absolute Value Inequalities

Click on the links below to jump directly to the relevant section

Exponents. Exponents tell us how many times to multiply a base number by itself.

if and if-else: Part 1

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

Adding and Subtracting Positive and Negative Numbers

5.1 Radical Notation and Rational Exponents

Example of a Java program

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Solutions of Linear Equations in One Variable

15 Prime and Composite Numbers

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

MATH 60 NOTEBOOK CERTIFICATIONS

8 Primes and Modular Arithmetic

Binary Adders: Half Adders and Full Adders

Common Beginner C++ Programming Mistakes

Chapter 5. Selection 5-1

Domain of a Composition

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Chapter 6. Linear Programming: The Simplex Method. Introduction to the Big M Method. Section 4 Maximization and Minimization with Problem Constraints

Basic Logic Gates Richard E. Haskell

Method To Solve Linear, Polynomial, or Absolute Value Inequalities:

Order of Operations More Essential Practice

MATH-0910 Review Concepts (Haugen)

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

A positive exponent means repeated multiplication. A negative exponent means the opposite of repeated multiplication, which is repeated

Common Multiples. List the multiples of 3. The multiples of 3 are 3 1, 3 2, 3 3, 3 4,...

Accentuate the Negative: Homework Examples from ACE

Linear Programming Notes V Problem Transformations

This explains why the mixed number equivalent to 7/3 is 2 + 1/3, also written 2

Notes for EER #4 Graph transformations (vertical & horizontal shifts, vertical stretching & compression, and reflections) of basic functions.

Very simple: the phrase 'can I' followed by the verb. But what verb form comes after the phrase 'can I'? Listen to these two examples of asking.

Section 1. Inequalities

Learning Objectives for Section 1.1 Linear Equations and Inequalities

Using Casio Graphics Calculators

26 Integers: Multiplication, Division, and Order

Exponents, Radicals, and Scientific Notation

This is a square root. The number under the radical is 9. (An asterisk * means multiply.)

Algebra Cheat Sheets

Linear Equations and Inequalities

Object Oriented Software Design

SIMPLIFYING ALGEBRAIC FRACTIONS

UNIT 3 VOCABULARY: INTEGERS

A.2. Exponents and Radicals. Integer Exponents. What you should learn. Exponential Notation. Why you should learn it. Properties of Exponents

Mathematical Induction

DDBA 8438: Introduction to Hypothesis Testing Video Podcast Transcript

Scientific Notation. Section 7-1 Part 2

1.6 The Order of Operations

Pre-Algebra - Integers

Welcome to Basic Math Skills!

Math Workshop October 2010 Fractions and Repeating Decimals

SAT Math Facts & Formulas Review Quiz

So far we have investigated combinational logic for which the output of the logic devices/circuits depends only on the present state of the inputs.

Radicals - Rational Exponents

So let us begin our quest to find the holy grail of real analysis.

NUMBER SYSTEMS. William Stallings

Solving Rational Equations

Chapter 4 -- Decimals

ChE-1800 H-2: Flowchart Diagrams (last updated January 13, 2013)

Determine If An Equation Represents a Function

3.1 Solving Systems Using Tables and Graphs

FRACTIONS COMMON MISTAKES

Exponents and Radicals

Indices and Surds. The Laws on Indices. 1. Multiplication: Mgr. ubomíra Tomková

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

MATH 13150: Freshman Seminar Unit 10

Algebraic expressions are a combination of numbers and variables. Here are examples of some basic algebraic expressions.

Introduction to Macros and Visual Basic in Excel

CAHSEE on Target UC Davis, School and University Partnerships

The Order of Operations Redesigned. Rachel McCloskey Dr. Valerie Faulkner

Lesson 4. Factors and Multiples. Objectives

Operations with positive and negative numbers - see first chapter below. Rules related to working with fractions - see second chapter below

Computer Science 281 Binary and Hexadecimal Review

Square Roots and Other Radicals

Functions and their Graphs

The Function Game: Can You Guess the Secret?

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

What Fun! It's Practice with Scientific Notation!

Decimals and other fractions

Math 0980 Chapter Objectives. Chapter 1: Introduction to Algebra: The Integers.

6.4 Normal Distribution

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Toothpick Squares: An Introduction to Formulas

Core Maths C1. Revision Notes

Solving Linear Equations - General Equations

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Mathematics. What to expect Resources Study Strategies Helpful Preparation Tips Problem Solving Strategies and Hints Test taking strategies

Visual Basic Programming. An Introduction

Beginning Algebra Math 100B. Math Study Center BYU-Idaho

**Unedited Draft** Arithmetic Revisited Lesson 5: Decimal Fractions or Place Value Extended Part 3: Multiplying Decimals

Transcription:

Boolean Logic in MATLAB When programming, there will be times when you want to control the flow of your code based on certain events occurring or certain values being reached. Primarily, this is handled through "if" statements and "while" statement (also known as "while" loops or "do while" loops). The system for evaluating logical operations (that is, to check whether or not some specified condition is met) is known as Boolean Logic. Don't let the name make you wary; this can be boiled down to little more than checking whether or not a given statement is true or false, along with using the operations AND, OR, and NOT. You've probably done this before in other classes. Some programming languages have special Boolean variables "True" and "False" to be used exclusively in these Boolean Logic operations. MATLAB is different: Like many mathematicians, it uses the value 0 for "False" and 1 for "True." MATLAB actually goes one step further and uses any number other than 0 for "True." Basic Logical Comparisons When programming, there will be times when you want to check whether a certain variable has reached a certain value. This is when you will make a logical comparison. All this means is that you will ask the program to evaluate whether a certain statement is true or false. For example; 12 < 5. If you type this into MATLAB, MATLAB will return "0," meaning the statement is false. You will often use inequalities for your logical comparisons. "Less than" and "greater than" are entered simply with < or >, respectively. "Less than or equal to" and "greater than or equal to" are entered in MATLAB by <= or >=, respectively. Some examples (try these in MATLAB if you would like): 4 > 1 12<=12 13>= 3 9 <1e6 3+4<2*3-4 If you enter these into MATLAB, you should notice two things: 1) Adding spaces before or after the inequality signs doesn't make a difference. 2) The determination of "True" or "False" is made after the statements on both sides have been evaluated. In other words, logical comparisons are at the bottom (or end) of the order of operations (PEMDAS). This means that you don't have to throw tons of parentheses into your code! (3+4) < ((2*3)-4) is the same as 3+4 < 2*3-4 (Also, the "e" above is MATLAB's scientific notation. 1e6 is the same as 1*10^6).

There are two other basic logical comparisons that you will use in programming: Equals and "Not Equals." Normally, one might say "Inequality" instead of "Not Equals," but I already used "inequality" above talking about comparisons such as < and >=, so I didn't want there to be any confusion. When checking for equality, remember that "=" is used to assign variables in MATLAB. If you were to type 3 = 3 into MATLAB, you would receive an error. To check if two values are equal, you must use "==" (two equal signs). This is very easy to forget when programming, so some programmers recommend replacing an "equals" check with a "not not equal" check. For information on this and on the "Not Equals" check, see the section on NOT below. Basic Logical Operations: AND, OR, and NOT. Just like we can addition, subtraction, multiplication, and division are basic operations on numbers, there are three basic operations that we can use on the values "True" and "False," and they each have their own special symbols. AND The AND operation takes two logical values (Boolean values) and returns "True" if and only if both values are "True." The symbol for AND in MATLAB (and many other programming languages) is &&. Some examples: 1 && 0 0 && 0 1 && 1 There is also an "and" command in MATLAB that does the same thing, but it's not used very often. and(1,0) and(0,0) and(1,1) Remember, however, that MATLAB uses 0 and 1 for "False" and "True," so we can use our basic numerical operations on Boolean values in MATLAB! Specifically, multiplication is logically equivalent to AND. Think about it this way: If we multiply two numbers together and get 0, then at least one of them was 0. If we multiply two numbers together and get something that is not 0, then neither of the numbers was 0. This is the same as AND: We get 1 ("not zero") if and only if neither Boolean value is 0. For this reason, we can freely use * in place of &&. As one final assurance that these two operations are equivalent, compare the following truth tables:

&& True False True False * 1 0 1 0 OR The OR operation takes two Boolean values and retuns "True" if and only if at least one value is "True." The symbol for OR in MATLAB (and many other programming languages) is (two vertical lines or "pipes," shift + \). Some examples: 1 0 0 0 1 1 There is also an "or" command just like the "and" command, and it is not used very often, Also, since we can use numerical operations on Boolean values in MATLAB, we find that addition is almost logically equivalent to OR. As long as none of our values are negative, then addition is logially equivalent to OR. Think about it this way: If we add two non-negative numbers together and get 0, then both numbers must have been 0. If we add two non-negative numbers together and get something other than 0, then at least one of them had to be greater than 0. This is the same as OR. Thus, if we're careful about what numbers appear, we can use + in place of. Once again, truth tables: True False True False

+ 1 0 1 2 0 (remember that 2 is also "True" in MATLAB). NOT The final basic Boolean operation is NOT: It takes one Boolean value and returns the opposite value. "NOT True" is "False," and "NOT False" is "True." The symbol for NOT in MATLAB (and some other programming languages) is ~ (tilde). (Just a note: the! (exclamation mark) seems to be used most often for the NOT operator in programming languages). The NOT operator will also stack with itself, which is mostly used when you want to convert a number other than 1 or 0 into 1. Some examples: ~0 ~1 ~4 ~-3 ~~0 ~~1 ~~580 ~~-38.4 ~(3<4) ~(4==10) Not Equals Now that we've introduced the NOT operator, we can more easily introduce the "Not Equals" operator: ~=. Some examples: 1 ~= 1 3 ~= -4 The fact that "=" is used for assigning a value to a variable and "==" is used for logical comparison can cause problems if you're not very careful in your coding. For this reason, many programmers replace an "Equals" check with a "not not equals," that is, ~(a ~= b). Some examples: ~(5~=5) 5==5 ~(3~=4) 3==4

Whichever you use, just be careful!