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
v
and stores the value in a variable namedfirst
- using indexing, gets the second element of
v
and stores the value in a variable namedsecond
- using indexing, gets the last element of
v
and stores the value in a variable namedlast
. Use the indexend
to get the last element. - using indexing, gets the second last element of
v
and stores the value in a variable namednextToLast
. Use the indexend - 1
to get the second last element. - gets the number of elements in
v
and stores the value in a variable namedlen
- using indexing, gets the last element of
v
and stores the value in a variable namedalsoLast
. Use the indexlen
to get the last element.