Skip to main content
Engineering LibreTexts

4.10: Common Vector Operations

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

    We’ve covered some of the basic features of vectors. Let’s now look at some common patterns we use to work with data stored in vectors.

    Reduce

    We frequently use loops to run through the elements of a vector and add them up, multiply them together, compute the sum of their squares, and so on. This kind of operation is called reduce, because it reduces a vector with multiple elements down to a single number.

    For example, the loop in Listing 4.2 adds up the elements of a vector named X (which we assume has been defined).

    List 4.2: Reducing a vector to a single scalar value (the sum)

    total = 0
    for i=1:length(X)
        total = total + X(i)
    end
    ans = total

    The use of total as an accumulator is similar to what we saw in Chapter 3. Again, we use the length function to find the upper bound of the range, so this loop will work regardless of the length of X. Each time through the loop, we add in the ith element of X, so at the end of the loop total contains the sum of the elements.

    MATLAB provides functions that perform some reduce operations. For example, the sum function computes the sum of the elements in a vector, and prod computes the product.

    Apply

    Another common use of a loop is to run through the elements of a vector, perform some operation on the elements, and create a new vector with the results. This operation is called apply, because you apply the operation to each element in the vector.

    For example, the loop in Listing 4.3 creates a vector Y that contains the squares of the elements of X (assuming, again, that X is already defined).

    Listing 4.3: Making a new vector Y by squaring the elements in X

    for i=1:length(X)
        Y(i) = X(i)^2
    end

    Many apply operations can be done with element-wise operators. The following statement is more concise than the loop in Listing 4.3.

    Y = X .^ 2

    It also runs faster!


    This page titled 4.10: Common Vector Operations 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; a detailed edit history is available upon request.