• No results found

Boolean Logic in MATLAB

N/A
N/A
Protected

Academic year: 2021

Share "Boolean Logic in MATLAB"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

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:

(3)

&& 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

(4)

+ 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

(5)

References

Related documents

Because complex Boolean statements can get tricky to think about, we can create a truth table to keep track of what truth values for the simple statements make the complex

It is possible that a number of laws and regulations may be adopted in the United States and elsewhere that could restrict the wireless communications industry or further regulate

[r]

In this review, the research carried out using various ion-exchange resin-like adsorbents including modified clays, lignocellulosic biomasses, chitosan and its derivatives, microbial

While in Table 3 we present a pooled specification, to increase the chances for the added variables to exert a significant impact, in unreported regressions we repeat the

university reform claims that strategic manage- ment has been strengthened in the universities, while the role of university per- sonnel has remained weak. Two major strategy

GAA - Georgia Alternate Assessment GaDOE - Georgia Department of Education GHSGT - Georgia High School Graduation Tests GHSWT - Georgia High School Writing Test..