Skip to main content
Contents
Calc
Dark Mode Prev Next Profile
\(\newcommand\DLGray{\color{Gray}}
\newcommand\DLO{\color{BurntOrange}}
\newcommand\DLRa{\color{WildStrawberry}}
\newcommand\DLGa{\color{Green}}
\newcommand\DLGb{\color{PineGreen}}
\newcommand\DLBa{\color{RoyalBlue}}
\newcommand\DLBb{\color{Cerulean}}
\newcommand\ds{\displaystyle}
\newcommand\ddx{\frac{d}{dx}}
\newcommand\os{\overset}
\newcommand\us{\underset}
\newcommand\ob{\overbrace}
\newcommand\obt{\overbracket}
\newcommand\ub{\underbrace}
\newcommand\ubt{\underbracket}
\newcommand\ul{\underline}
\newcommand\tikznode[3][]
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};
}
\newcommand\del{\nabla}
\newcommand\R{\mathbb{R}}
\newcommand\C{\mathcal{C}}
\newcommand\N{\mathcal{N}}
\newcommand\eps{\varepsilon}
\renewcommand\epsilon{\varepsilon}
\renewcommand\subset{\subseteq}
\newcommand\norm[1]{\|{#1}\|}
\newcommand\matrixbrackets[4][1]{
\draw (#3,#2) -- (\fpeval{#3+0.2},#2);
\draw (#3,#1) -- (#3 ,#2);
\draw (#3,#1) -- (\fpeval{#3+0.2},#1);
\draw (#4,#2) -- (\fpeval{#4-0.2},#2);
\draw (#4,#1) -- (#4 ,#2);
\draw (#4,#1) -- (\fpeval{#4-0.2},#1);
}
\tikzstyle{circle node 0}=[fill=white, draw=black, shape=circle, inner sep=0pt]
\tikzstyle{circle node 2}=[fill=white, draw=black, shape=circle, inner sep=2pt]
\tikzstyle{hrect node}=[fill=white, draw=black, inner sep=2pt, outer sep=3pt]
\tikzstyle{vrect node}=[fill=white, draw=black, inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 0}=[inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 2}=[fill=white, inner sep=2pt, outer sep=2pt]
\tikzstyle{rect node 1}=[fill=white, inner sep=2pt, outer sep=0pt]
\tikzstyle{rect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Subsection Logical Statements (Questions)
A
logical statement is any MATLAB expression that produces a
logical value, either
true or
false. The most common logical statements come from comparing two values. These comparisons use
relational operators .
Table 19. Relational operators in MATLAB
==
x == y
Is \(\texttt{x}\) equal to \(\texttt{y}\text{?}\)
~=
x ~= y
Is \(\texttt{x}\) not equal to \(\texttt{y}\text{?}\)
<
x < y
Is \(\texttt{x}\) less than \(\texttt{y}\text{?}\)
<=
x <= y
Is \(\texttt{x}\) less than or equal to \(\texttt{y}\text{?}\)
>
x > y
Is \(\texttt{x}\) greater than \(\texttt{y}\text{?}\)
>=
x >= y
\(\text{Is}\ \texttt{x}\ \text{greater than or equal to}\ \texttt{y} \text{?}\)
A common beginner mistake is confusing the assignment operator
= with the comparison operator
==. The single equal sign assigns a value to a variable (telling MATLAB what to store), while the double equal sign compares two values (asking MATLAB a question).
cost = 7.1; % Telling: assign a value
cost == 7.1 % Asking: compare a value
Checkpoint 20 . Assignment vs. Comparison.
Which of the following statements is correct?
x = 5 sets x equal to 5, while x == 5 tests if x equals 5
Correct! The single equal sign is for assignment, and the double equal sign is for comparison.
x = 5 and x == 5 both do the same thing
Incorrect. These operators have completely different purposes: one assigns a value, the other asks a question.
x == 5 assigns 5 to x only if x doesnβt already have a value
Incorrect. The == operator never assigns values; it only compares them.
Here are a few comparison examples. The parentheses are optional, but they help clarify which parts of the expression are being compared.
a = 4;
b = -4;
ans1 = (a == 4); % β Is a equal to 4? Yes.
ans2 = (a == b); % β Is a equal to b? No.
ans3 = (b < a); % β Is b less than a? Yes.
This code defines two double variables, a and b, and three logical variables:
ans1 = 1 since the logical statement a == 4 is true.
ans2 = 0 since the logical statement a == b is false.
ans3 = 1 since the logical statement b < a is true.
Checkpoint 21 . Check Your Understanding: Logical Statements.
(a) Logical Statements.
Let
x,
y, and
z be numeric variables. Which of the following are valid logical statements in MATLAB?
x = y
Incorrect. The expression x = y uses a single equal sign, which is for assignment, not comparison.
z > y
Correct! The expression z > y is a valid logical statement that asks if z is greater than y.
y =< 2
Incorrect. The expression y =< 2 uses an incorrect operator; the correct operator for "less than or equal to" is <=.
z ~= z
Correct! The expression z ~= z is a valid logical statement that asks if z is not equal to z.
x ~ z
Incorrect. The expression x ~ z is not a valid logical statement because it lacks a proper relational operator.
(b)
What is the result of the following logical expression in MATLAB?
1 (logical)
Correct! Since 10 is indeed greater than 5, the expression evaluates to true (logical 1).
0 (logical)
Incorrect. Remember that 10 is greater than 5, so the expression evaluates to true.
1 (double)
Incorrect. While 1 (double) and true (logical 1) may seem similar, they are different data types in MATLAB.
0 (double)
Incorrect. The expression evaluates to true (logical 1), not false.
(c)
What is the result of the following logical expression in MATLAB?
1 (logical)
Correct! Since 3.14 is only an approximation of pi, it is not equal to pi.
0 (logical)
Incorrect. Since 3.14 is only an approximation of pi, it is not exactly equal to the value of pi in MATLAB, so the expression evaluates to true.
1 (double)
Incorrect. While 1 (double) and true (logical 1) may seem similar, they are different data types in MATLAB.
0 (double)
Incorrect. The expression evaluates to true (logical 1), not false, since pi and 3.14 are not exactly equal.
Negating Logical Statements.
To ask the opposite of a logical statement, use the NOT operator
~. If an expression is true, its negation is false, and vice versa.
~true % returns false
~false % returns true
The NOT operator negates a single logical value or statement. When the statement is a comparison, use parentheses so MATLAB negates the whole comparison.
For example, the following statements ask if β8 is not equal to 9β:
Similarly, the negation of x >= 5 is x < 5, so the two statements are equivalent:
When negating expressions, use parentheses to make your intent clear and improve the readability of your code.
Checkpoint 22 . Negating Logical Statements.
Which of the following expressions correctly asks "Is
x NOT greater than 10?" Select all that apply.
~(x > 10)
Correct! This uses the NOT operator to negate the comparison.
x <= 10
Correct! This is the equivalent relational operator form without negation.
~x > 10
Incorrect. Without parentheses, this negates x first, then compares the result to 10, which is not the intended meaning.
x < 10
Incorrect. This asks if x is strictly less than 10, which excludes the case where x equals 10.
Joining Two Statements.
Many questions involve multiple conditions. For example, to determine if a number is an even number greater than 100, then you need to check two things at once:
Because both must be true, we join these statements with βandβ. If either of these conditions could be true, then we would join them with βorβ.
MATLAB joins logical statements with AND and OR using the operators
& (AND) and
| (OR).
Table 23. Logical operators for combining statements
\(\textbf{Logical}\) \(\textbf{operator}\)
\(\textbf{Logical statement}\)
\(\textbf{Question being asked}\)
&
Are both \(\texttt{ans1}\) and \(\texttt{ans2}\) true?
|
ans1 | ans2
\(\text{Is at least one of}\ \texttt{ans1}\ \text{or}\ \texttt{ans2}\ \text{true?}\)
The result of joining logical statements are exactly what you would expect from everyday language: AND is true only when both sides are true, while OR is true when at least one side is true.
% Quick checks
true & true % β true: 1 (logical)
true & false % β false: 0 (logical)
false & true % β false: 0 (logical)
false & false % β false: 0 (logical)
true | true % β true: 1 (logical)
true | false % β true: 1 (logical)
false | true % β true: 1 (logical)
false | false % β false: 0 (logical)
Now try these operators in context.
P = 1;
Q = 10;
R = -4;
P <= R & P > Q % β false & false: 0 (logical)
R < 0 & R < Q % β true & true: 1 (logical)
Q == R | Q == P % β false | false: 0 (logical)
P < Q | P < R % β true | false: 1 (logical)
~(P < Q & P < R) % β NOT (true & false): 1 (logical)
P > Q | P > R % β false | true: 1 (logical)
Checkpoint 24 . Combining Logical Statements.
Given
age = 25 and
hasLicense = true, which expression is the
most readable way to ask "Is the person at least 18 years old AND has a driverβs license?"
(age >= 18) & hasLicense
Correct! This is clear and readable, using the AND operator directly with meaningful variable names.
(age >= 18) | hasLicense
Incorrect. The OR operator returns true if either condition is true, but we need both conditions to be true.
age >= 18 & hasLicense == true
While logically correct, comparing hasLicense == true is redundant and less readable. Use hasLicense directly since itβs already a logical value.
~(age < 18 | ~hasLicense)
While logically equivalent (using De Morganβs Law), this is much harder to read. Prefer the straightforward AND version for clarity.
Joining Many Statements.
You can combine more than two logical statements, and you can mix
~,
&, and
| in the same expression. When you do, MATLAB evaluates operators in a fixed order. Parentheses override everything, so you can always force the grouping you intend.
In general, the logical operator precedence chain is as follows:
1st
2nd
3rd
4th
()
β―
~
β―
&
β―
|
Table 25. Sample Logical Operator Precedence Interpretations
MATLAB Command
Interpretation
~A & B
β
(~A) & B
~A | B
β
(~A) | B
A & B | C
β
(A & B) | C
A | B & C
β
A | (B & C)
When in doubt, use parentheses. They make your code clearer to readers and reduce errors caused by misinterpreting precedence.
Checkpoint 26 . Operator Precedence.
According to MATLABβs operator precedence rules, which expression illustrates how
A | ~B & C | D is evaluated?
A | ((~B) & C) | D
Correct! The NOT operator ~ is evaluated first, followed by the AND operator &, and finally the OR operators | are evaluated from left to right.
(A | (~B)) & (C | D)
Incorrect. The AND operator & has higher precedence than the OR operator |, so this grouping is not correct.
((A | ~B) & C) | D
Incorrect. The AND operator & is evaluated before the OR operator |, but the NOT operator ~ is evaluated first.
A | ~(B & C) | D
Incorrect. The AND operator & is evaluated before the OR operator |, but the NOT operator ~ is evaluated first.
Exercises π€π Conceptual Questions
1.
(a) Logical Data Types.
In MATLAB,
true and
1 are the same.
True.
Incorrect. While true and 1 are represented by the same numeric value, they have different data types: true is of type logical, while 1 is of type double.
False.
Incorrect. While true and 1 are represented by the same numeric value, they have different data types: true is of type logical, while 1 is of type double.
(b) Relational Operators: Inequality.
Which of the following commands test that
x is not equal to
y?
~(x = y)
~(x == y)
~x == y
x ~= y
x ~== y
(c) Translating Relational Operators.
(d) Basic Logical Operators.
Select the value contained in
ans after evaluating the following expression:
ans = (3 >= 2) & (3+4 == 6)
ans = 1 (logical)
Incorrect. While 3 >= 2 is true, check the second part: is \(3+4\) really equal to \(6\text{?}\)
ans = 0 (logical)
Correct! Even though 3 >= 2 is true, the expression 3+4 == 6 is false (since \(3+4=7\) ). For an AND operation to be true, both sides must be true.
ans = 1
Incorrect. The result of a logical operation is always of type logical, not double.
ans = 0
Incorrect. The result of a logical operation is always of type logical, not double.
None of the above. This expression will produce an error.
(e) Understanding AND.
An AND operation (
&) returns true if at least one of the conditions is true.
True.
Incorrect. An AND operation requires both conditions to be true. Youβre thinking of OR (|), which returns true if at least one condition is true.
False.
Incorrect. An AND operation requires both conditions to be true. Youβre thinking of OR (|), which returns true if at least one condition is true.
(h) Combining with AND.
Select the command that is equivalent to asking the question:
Is
p a negative number greater than
-4?
p < 0 & p >= -4
p <= 0 | p > -4
-4 < p < 0
p < 0 | p > -4
p < 0 & p > -4
(i) Negating with NOT.
Which logical statements are equivalent to asking the question:
~(p > 0)
p <= 0
~p > 0
p ~> 0
p < 0
(k) Operator Precedence.
Select the logical statement that is equivalent to the following:
((~A) & B) | C
~(A & B | C)
~A & (B | C)
~(A & B) | C