Skip to main content

Registration is now open for this year's LibreFest! Join us virtually the week of July 13.

Register here
Engineering LibreTexts

3.12: Common Vector Operations

  • Page ID
    134996
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Rounding Functions

    Rounding functions convert floating-point values to nearby integer values, but each function follows a different rule.

    Function

    Description

    ceil(x)

    Rounds each element up to the nearest integer greater than or equal to the element

    floor(x)

    Rounds each element down to the nearest integer less than or equal to the element

    fix(x)

    Rounds each element toward zero

    round(x)

    Rounds each element to the nearest integer

     

    Example \(\PageIndex{1}\)

    Using rounding functions.

    array1 = [2.1, 5.7, -3.7, -1.2];
    
    value_1 = ceil(array1)
    
    value_2 = floor(array1)
    
    value_3 = fix(array1)
    
    value_4 = round(array1)
     
    Solution
    value_1 =
    
        3 6 -3 -1
    
    
    value_2 =
    
        2 5 -4 -2
    
    
    value_3 =
    
        2 5 -3 -1
    
    
    value_4 =
    
        2 6 -4 -1

     


     

     

    Caution

    Think carefully with negative numbers: ceil, floor, and fix behave differently for negative values. Try the examples above and compare the results.

     

    Remainder and Modulus

    MATLAB has two functions for computing remainders: rem and mod.  Both functions return the remainder of a division operation. They often produce the same result for positive numbers, but they can differ when negative numbers are involved.

     

    Example \(\PageIndex{2}\)

    Using rem and mod functions.

    x = [5, 10, 12, -13];
    y = 2;
    
    value_1 = rem(x, y)
    value_2 = mod(x, y)
    
    
    Solution
    value_1 =
    
         1     0     0    -1
    
    
    value_2 =
    
         1     0     0     1

    In the value_1 and value_2 arrays, notice the difference in the remainder when -13 is divided by 2 using the rem and mod functions. The rem function returns -1, while the mod function returns 1.


     

    A common use of mod is to test whether a number is even, odd, or divisible by another number.

     

    Example \(\PageIndex{3}\)

    Using rem and mod functions.

    values = 1:10;
    evens = values(mod(values, 2) == 0)
    odds  = values(mod(values, 2) == 1)
     
    Solution
    
    evens =
    
         2     4     6     8    10
    
    
    odds =
    
         1     3     5     7     9
    

     

     

    Statistics Functions for Arrays

    MATLAB includes many built-in functions for basic statistical analysis. These functions are especially useful when working with experimental data.

    Function

    Description

    mean(x)

    Average value

    std(x)

    Standard deviation, a measure of spread around the mean

    median(x)

    Middle value after sorting

    mode(x)

    Most frequently occurring value

    max(x)

    Largest value

    min(x)

    Smallest value

     

    Example \(\PageIndex{4}\)

    Add example text here.

    data = [9, 50, 51, 49, 100];
    value_1 = mean(data)
    value_2 = std(data)
    value_3 = median(data)
    value_4 = mode(data)
    
     
    Solution
    value_1 = 51.8000
    
    value_2 = 32.2754
    
    value_3 = 50
    
    value_4 = 9


     

     

    Random Numbers

    Random numbers are useful for simulations, testing programs, games, and probability experiments. MATLAB has two commonly used random-number functions: rand and randi.

    Function

    Description

    rand

    Creates a random number between 0 and 1

    rand(m,n)

    Creates an m-by-n array of random decimal values between 0 and 1

    randi(maxVal,m,n)

    Creates an m-by-n array of random integers from 1 to maxVal

     

    Example \(\PageIndex{5}\)

    Add example text here.

    value_1 = rand
    value_2 = rand(3)
    value_3 = rand(3, 1)
    value_4 = rand(1, 3)
    value_5 = randi(5, 3, 1)
    value_6 = randi(5, 3)
     
    Solution
    value_1 = 0.8147
    
    
    value_2 =
    
        0.9058    0.6324    0.5469
        0.1270    0.0975    0.9575
        0.9134    0.2785    0.9649
    
    
    value_3 =
    
        0.1576
        0.9706
        0.9572
    
    
    value_4 =
    
        0.4854    0.8003    0.1419
    
    
    value_5 =
    
         3
         5
         4
    
    
    value_6 =
    
         5     5     4
         4     5     4
         1     4     2


     

    Random Numbers in a Specific Range

    To create random decimal numbers between 4 and 9, start with rand, which gives values between 0 and 1, stretch the range by multiplying by 5, and then shift the range by adding 4.

    Example \(\PageIndex{6}\)
    randomDecimals = 4 + 5 * rand(1, 4)
    Solution
    randomDecimals =
    
        7.2774 4.8559 7.5302 4.1592


     

    To create random integers between 4 and 9, use randi carefully. Since there are six integers from 4 through 9, generate integers from 1 through 6 and then shift them by adding 3.

     

    Example \(\PageIndex{7}\)
    randomIntegers = 3 + randi(6, 1, 4)
    
    Solution
    randomIntegers =
    
         5     4     4     8

     

     


    3.12: Common Vector Operations is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?