7 Use indexing to get a single element of a vector
Create a new script and save it as q2.m.
In the newly created script, add the following:
% v is a vector of random integer values between 1 and 100
% v has between 5 and 10 elements
% everytime this script is run v may have a different
% number of elements and different element values
v = randi(100, [1 randi([5 10], 1)]);
To the script, add some MATLAB statements that perform the following steps in order:
- using indexing, gets the first element of
vand stores the value in a variable namedfirst - using indexing, gets the second element of
vand stores the value in a variable namedsecond - using indexing, gets the last element of
vand stores the value in a variable namedlast. Use the indexendto get the last element. - using indexing, gets the second last element of
vand stores the value in a variable namednextToLast. Use the indexend - 1to get the second last element. - gets the number of elements in
vand stores the value in a variable namedlen - using indexing, gets the last element of
vand stores the value in a variable namedalsoLast. Use the indexlento get the last element.