PracticeWithVectors

Telechargé par amine maiza
Practice with Vectors
So far, you've learned some fundamental operations to create and work with vectors. In this live script, you'll
review earlier concepts and learn new vector operations. The activities are divided into the following sections:
Table of Contents
Creating Vectors.......................................................................................................................................................1
Concatenation.......................................................................................................................................................1
Vectors with Set Spacing......................................................................................................................................2
Vectors with Set Number of Elements.................................................................................................................. 3
Summary.............................................................................................................................................................. 3
Accessing and Manipulating Vector Elements..........................................................................................................3
Vector Indexing.....................................................................................................................................................3
Vector Manipulation.............................................................................................................................................. 4
Math Operations with Vectors...................................................................................................................................4
Arithmetic Operations...........................................................................................................................................4
Math Functions.....................................................................................................................................................5
Vector Characteristics...............................................................................................................................................6
Transpose.............................................................................................................................................................6
Size and Length....................................................................................................................................................6
Conclusion................................................................................................................................................................7
Creating Vectors
There are many ways to create vectors. In this section, you will see how to
Create vectors using concatenation.
Create vectors with a set spacing.
Create vectors with a set number of elements.
Concatenation
Square brackets are used to group elements together into vectors. Use a space between elements to create a
row vector, or add a semicolon between elements to create a column vector.
rowVector = [1 3 6 3 11] % Row vector
colVector = [1; 3; 6; 3; 11] % Column vector
Use concatenation to create a column vector v1 with the following elements: 0, 1, 1, 0.
v1=[0; 1; 1; 0]
1
v1 = 4×1
0
1
1
0
Create a second column vector v2 with the following elements: 0,5,0.
v2=[0;5;0]
v2 = 3×1
0
5
0
Square brackets are also used to combine vectors together. Create a new column vector v3 that combines the
v1 and v2 vectors together. This should create a new column vector with seven rows and one column.
v3=[v1;v2]
v3 = 7×1
0
1
1
0
0
5
0
Vectors with Set Spacing
The colon operator creates a row vector with a set spacing. If you do not specify the spacing value, it will use a
default spacing of 1.
v = startingValue:spacing:endingValue
v = 0:2:8
Use the colon operator to create a vector named v4 that starts at 45, ends at 90, with a spacing of 5.
v4=[45:5:90]
v4 = 1×10
45 50 55 60 65 70 75 80 85 90
Use the colon operator to create a vector named v5 that starts at -10 and ends at 5, with the default spacing of
1.
v5=[-10:5]
2
v5 = 1×16
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2
Vectors with Set Number of Elements
The linspace function creates a vector with a set number of equally spaced elements:
v = linspace(startingValue,endingValue,numberOfElements)
v = linspace(0,8,5)
Use the linspace function to create a vector named v6 that starts at 0, ends at , with 5 equally spaced
elements. Use the pi function for the value of .
v6=linspace(0, 22*pi, 5)
v6 = 1×5
0 17.2788 34.5575 51.8363 69.1150
Summary
When creating vectors, choose the approach best suited for what you need to accomplish:
Use concatenation when you need to combine multiple elements or vectors into a single vector.
Use the colon operator when you need to create a vector with a set spacing between the elements.
Use the linspace function when you need to create a vector with a set number of elements.
Accessing and Manipulating Vector Elements
Vector Indexing
To access elements from a vector, add parenthesis after the vector name. Inside the parenthesis, specify the
indices of the desired elements. Use an equal sign (=) before the indexing operation to assign the result to a
variable.
v(3) % Get the third element
val1 = v(end) % Store the last element into a variable val1
val2 = v([5 7 4]) % Store the fifth, seventh, and fourth elements into a vector val2
val3 = v(1:2:end) % Store every other element, starting with the first, into a vector val3
3
Use indexing to obtain the fifth element from the vector v6 and store it in a variable named x.
v6(5)
ans = 69.1150
Vector Manipulation
Use an equal sign (=) after the indexing operation to assign new values to vector elements.
v(3) = 10; % Change value of 5th element to 10
v([1 4]) = [5 15]; % Change value of the 1st element to 5 and the value of the 4th element to 15
v(3:end) = 0; % Change the value to 0 for every element from the 3rd index to the end of the vector
Change the third value of the v6 vector to 0.
v6(3)=0
v6 = 1×5
0 17.2788 0 51.8363 69.1150
Math Operations with Vectors
Arithmetic Operations
Use elementwise operators to perform mathematical operations on the corresponding elements of two vectors.
Below are examples of common math operators in MATLAB.
[1; 3] + [5; 4] = [6; 7] % Addition
[1; 3] - [5; 4] = [-4; -1] % Subtraction
[1; 3] .* [5; 4] = [5; 12] % Elementwise multiplication
[1; 3] ./ [5; 4] = [0.2; 0.75] % Elementwise division
[1; 3] .^ [5; 4] = [1; 81] % Elementwise power
4
Given the vectors A and B, compute the elementwise multiplication of A times B. Store the result in a new vector
named C.
A = [1; 28; 40];
B = [16; 35; 120];
c=A.*B
c = 3×1
16
980
4800
Math Functions
There are many common mathematical functions in that operate on vectors. The function is automatically
applied to every element of the vector.
sin(x), cos(x), tan(x) % Trigonometric operators
exp(x), log(x), log10(x) % Exponential, natural log, and base-10 log
abs(x) % Absolute value
sqrt(x) % Square root
Given the vectors expected and experimental defined below, compute the percent error for each element
using the equation below.
Store the result in a new row vector named percentError. Use the abs function to compute the absolute
value.
expected = [50 55 60 65 70 75];
experimental = [48.8 55.2 54 62.8 65.7 74.6];
percentError=abs((experimental-expected)./expected)*100
percentError = 1×6
2.4000 0.3636 10.0000 3.3846 6.1429 0.5333
The resulting row vector should have six unique values ranging from about 0.36 to 10.
5
1 / 7 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !