4.11: Common Vector Functions
- Page ID
- 84399
\( \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}}\)
\( \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{\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}\)By Carey A. Smith
Additional MATLAB vector functions:
Functions that compute a single number from a vector
% This is vector of CO2 levels (ppm) measured at Mauna Loa Hawaii in 2018:
CO2 = [407.96, 408.32, 409.39, 410.25, 411.24, 410.79, 408.70, 406.97, 405.52, 406.00, 408.02, 409.08];
% Reduce functions:
CO2_sum = sum(CO2) % Total
CO2_length = length(CO2) % The number of items in the vector
CO2_min = min(CO2) % Minimum
CO2_max = max(CO2) % Maximum
Note: numel(x) give the same answer as length(x) when x is a vector. For a a matrix or array, they give different answers. I this book, length will only be used for vectors.
Solution
N/A
This is vector of CO2 levels (ppm) measured at Mauna Loa Hawaii in 2018:
CO2 = [407.96, 408.32, 409.39, 410.25, 411.24, 410.79, 408.70, 406.97, 405.52, 406.00, 408.02, 409.08];
% Apply functions and operators:
CO2_sqrt = sqrt(CO2) % Square roots of the elements
CO2_square = CO2.^2 % Squares of the elements (element-wise)
CO2_log10 = log10(CO2) % logarithm, base 10
CO2_log = log(CO2) % logarithm, base e (natural logarithm)
% log() should be ln(), but this is a long tradition in programming languages.
CO2_exp = exp(CO2) % e^x
CO2_column=CO2' % The ' symbol transposes a
vector
Note that sin(theta), cos(theta), tan(theta) are for theta in radians.
theta = pi*(0 : 0.1 : 0.3)
theta_sin = sin(theta)
theta_cos = cos(theta)
theta_tan = tan(theta)
Note that sind(alpha), cosd(alpha), tand(alpha) are for alpha in degrees.
alpha = 0 : 15 : 45
sin_alpha = sind(alpha)
cos_alpha = cosd(alpha)
tan_alpha = tand(alpha)
The attached file, MatlabCommands_Brian_Vick.pdf, courtesy of Dr. Brian Vick from Virginia Tech, is a very useful summary of the the most common MATLAB commands.
Solution
Add example text here.
Create this vector: v = [3, 4, 5, 6, 7]
Then write 1 line of cose that computes the square root of the sum of the squares of the elements of the vector, without using a loop.Write 1 line of code that
- Answer
-
Use the vector operator v.^2 to square each element of the vector; sum the elements sum(); compute the squareroot.
(1 pt) Create a Matlab script: sin_vector_YourName.m
(1 pt) Clear any variables; close any figures; eliminate white space; clear the console with this line of code:
clear all; close all; format compact; clc;
%% Part 1
(1 pt) Create a vector called degrees = 0 to 90 in steps of 10 degrees
degrees = 0 : 10 : 90;
(2 pts) Use sind() to compute the sines of the degrees vector.
(2 pts) Create a 2nd vector: radians = (2*pi/360)*degrees.
(2 pts) Use sin() to compute the sines of the radians vector.
%% Part 2
(2 pts) Convert the radians vector created in part 1 from a row vector
to a column vector with this code:
radians_col = radians' % The ' transposes a vector
(1 pt) Use cos() to compute the cosine this column radians vector.
- Answer
-
Add texts here. Do not delete this text first.
.
Link to additional function information
In the "Back Matter" of this book, there is a section called "MATLAB Commands and Functions". This has an attachment with short description of many MATLAB command and functions. This is a good place to search for a function that would be useful for a particular program.
MATLAB Functions, Commands and Special Characters
You can also use the MATLAB help browser to find useful functions.
.