4.3: Vector Arithmetic

Matlab and Octave Programming for STEM Applications · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    4.3: Vector Arithmetic

    Allen B. Downey's text:

    As with numbers, you can do arithmetic with vectors. If you add a number to a vector, MATLAB increments each element of the vector:

    X = 1:3
    Y = X + 5
    % Y = 6     7     8

    The result is a new vector; the original value of X has not changed.

    If you add two vectors, MATLAB adds the corresponding elements of each vector and creates a new vector that contains the sums:

    Z = X + Y
    % Z = 7     9    11

    But adding vectors only works if the operands are the same size. Otherwise you get an error:

    >> W = [1 2]
    W = 1     2
    
    >> X + W
    Matrix dimensions must agree.

    This error message might be confusing, because we think of X and W as vectors, not matrices. But in MATLAB, a vector is a kind of matrix. We’ll come back to matrices later, but in the meantime, you might see the term in error messages and documentation.

    If you divide two vectors, you might be surprised by the result:

    >> X / Y
    ans = 0.2953

    MATLAB is performing an operation called right division, which is not what we expected. To divide the elements of X by the elements of Y, you have to use ./, which is element-wise division:

    >> X ./ Y
    ans = 0.1667    0.2857    0.3750

    Multiplication has the same problem. If you use *, MATLAB does matrix multiplication. With these two vectors, matrix multiplication is not defined, so you get an error:

    >> X * Y
    Error using  *
    Incorrect dimensions for matrix multiplication.
    Check that the number of columns in the first matrix
    matches the number of rows in the second matrix.
    To perform element-wise multiplication, use '.*'.

    In this case, the error message is pretty helpful. As it suggests, you can use .* to perform element-wise multiplication:

    >> X .* Y
    ans = 6    14    24

    As an exercise, see what happens if you use the exponentiation operator (^) with a vector.

    Carey Smith's additional notes:

    In both MATLAB and Octave, all of these vector multiplication expressions are valid and give the same results:

    X.*Y   % No space between X and .* and no space between .* and Y
    X.* Y  % No space between X and .* but a space between .* and Y

    X .* Y % A space between X and .* and a space between .* and Y

    But this expression fails:

    X .*Y  % A space between X and .* but no space between .* and Y
     

    In both MATLAB and Octave, all of these vector division expressions are valid and give the same results:

    X./Y   % No space between X and ./ and no space between ./ and Y
    X./ Y  % No space between X and ./ but a space between ./ and Y

    X ./ Y % A space between X and ./ and a space between ./ and Y

    But this expression fails:

    X ./Y  % A space between X and ./ but no space between ./ and Y
     

    In both MATLAB and Octave, all of these vector exponential expressions are valid and give the same results:

    X.^3   % No space between X and .^ and no space between .^ and Y
    X.^ 3  % No space between X and .^ but a space between .^ and Y

    X .^ 3 % A space between X and .^ and a space between .^ and Y

    But this expression fails:

    X .^3  % A space between X and .^ but no space between .^ and Y

     


    This page titled 4.3: Vector Arithmetic was last modified on Wed, 07 Feb 2024 05:11:15 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform.