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 |
Using rounding functions.
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
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.
Using rem and mod functions.
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.
Using rem and mod functions.
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 |
Add example text here.
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 |
Add example text here.
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.
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.
Solution
randomIntegers =
5 4 4 8

