4.11: Common Vector Functions
- Page ID
- 84399
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
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.
.