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.