Skip to main content
Engineering LibreTexts

4.7: Relational and Logical Operations

  • Page ID
    55649
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Relational Operations

    A relational operator performs some kind of comparison between two variables (typically floating point double) and then returns a logical variable to indicate the outcome of the comparison. The relational operators are equal, \(==\); not equal, \(=;\) less than, \(<;\) greater than, \(>\); less than or equal, <=; greater than or equal, \(>==\). Note that the equal operator, \(==\), is now interpreted in the mathematical sense of \(=\) (vs. the MATLAB assignment operator \(=\) which of course serves a different function).

    As an example, we consider

    >> x = 4.6; y = 4.7;
    >> isless = x < y
    
    isless =
        1
    >>
    

    Here MATLAB first evaluates the expression \(\mathrm{x}<\mathrm{y}-\) which, since \(\mathrm{x}\) is less than \(\mathrm{y}\), is a true statement, and hence returns a logical 1 - and then assigns this value of 1 to the (logical) variable isless. (As we know, floating point numbers are truncated in finite precision arithmetic; since the < operates on the truncated form, of course the comparison is only good to machine precision.)

    We can of course consider composition. Typically, composition of relational operations is by logical operations, as we now discuss. (In fact, we can apply usual arithmetic to logical variables, which are converted to floating point for the purpose. This is often convenient, but must be used with some care.)

    Logical Operations

    It would be possible to hi-jack operations such as \(+\) to perform Boolean operations on logical variables, however Matlab prefers to reserve the usual arithmetic functions for these operators applied to logical variables. Hence we introduce new operators.

    The three key logical operators are AND, indicated as &, OR, indicated as \(|\), and NOT, indicated as ~. (There are also other options, such as the exclusive or, or XOR.) The AND and OR operators take as (the two) operands logical variables and the result is of course also a logical variable. The NOT takes a single logical operand and the result is also a logical variable. As already indicated, these logical operators are often composed with relational operators.

    The AND, OR, and NOT behave in the expected fashion. The statement L3 = L1 & L2 yields L3 \(=1\) (true) only if both L1 and L2 are both \(==1\) (true), otherwise L3 \(=0\). The statement L3 = L1 | L2 yields L3 \(=1\) if either L1 \(==1\) or L2 \(==1\), otherwise L3 = 0 (note that if both L1 \(==1\) and L2 \(==1\), then L3 \(=1\) ); conversely, L3 \(=0\) only if both L1 \(==0\) and L2 \(==0\). Finally, the NOT: L3 \(=\sim\) L1 yields L3 \(=1\) if L1 \(==0\) and L3 \(=0\) if L1 \(==1\).

    As an example, we consider the AND and NOT:

    >> x = 4.6; y = 4.7;
    >> u = 3.1; v = 3.2;
    >> z = (x < y) & ~(u < v)
    z =
        0
    >>
    

    which of course yields the correct result. Note the Matlab precedence convention is to evaluate relational operators before logical operators and hence our parentheses in the above are redundant — but better too many parentheses than too few. And another example,

    >> x = 4.6; y = 4.7;
    >> u = 3.1; v = 3.2;
    >> z = (x < y) | ~(u < v)
    z =
        1
    >>
    

    now with the OR and NOT.

    Finally, we mention that MATLAB provides "short-circuit" versions of AND and OR, the operators for which are given by && and | |, respectively. These short-circuit operators will not evaluate the second operand (i.e., to the right) if the first operand (i.e., to the left) suffices to make the decision. For example, in our OR example above, since \(\mathrm{x}<\mathrm{y}, \mathrm{z}=(\mathrm{x}<\mathrm{y})|| \sim(\mathrm{u}<\mathrm{v})\) will only evaluate \((x<y)\). This can lead to efficiencies \(-\) if the second operand is much more expensive and or ensure that a (second) operand is only evaluated when it exists and is well-defined.

    Logical (and relational) operations play a key role in flow control - in controlling the flow (or "direction") of the program based on the results generated by the program itself. In some sense it is these logical and relations operations and the enabled flow control which distinguishes a program executed by a computer from arithmetic performed on a calculator.


    This page titled 4.7: Relational and Logical Operations is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Masayuki Yano, James Douglass Penn, George Konidaris, & Anthony T Patera (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.