Skip to main content
Library homepage
 
Engineering LibreTexts

2.11: Assignment and Equality

  • Page ID
    85946
  • \( \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}}\)

    For beginning programmers, a common source of confusion is assignment and the use of the equals sign.

    In mathematics, the equals sign means that the two sides of the equation have the same value. In MATLAB, an assignment statement looks like a mathematical equality, but it’s not.

    One difference is that the sides of an assignment statement are not interchangeable. The right side can be any legal expression, but the left side has to be a variable, which is called the target of the assignment. So this is legal:

    >> y = 1;
    >> x = y + 1
    x = 2

    But this is not:

    >> y + 1 = x
     y + 1 = x
           |
    Error: Incorrect use of '=' operator.
    To assign a value to a variable, use '='.
    To compare values for equality, use '=='.

    In this case the error message is not very helpful. The problem here is that the expression on the left side is not a valid target for an assignment.

    Another difference between assignment and equality is that a mathematical equality is true (or false) for all eternity; an assignment statement is temporary. When you assign x = y + 1, you get the current value of y. If y changes later, x does not get updated.

    A third difference is that a mathematical equality is a statement that may or may not be true. In mathematics, \(y = y+1\) is a statement that happens to be false for all values of \(y\). In MATLAB, y = y + 1 is a sensible and useful assignment statement. It reads the current value of y, adds 1, and replaces the old value with the new value.

    >> y = 1;
    >> y = y + 1
    y = 2

    When you read MATLAB code, you might find it helpful to pronounce the equals sign as “gets” rather than “equals.” So x = y + 1 is pronounced “x gets the value of y plus one.”


    This page titled 2.11: Assignment and Equality is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.