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:

  1. using indexing, gets the first element of v and stores the value in a variable named first
  2. using indexing, gets the second element of v and stores the value in a variable named second
  3. using indexing, gets the last element of v and stores the value in a variable named last. Use the index end to get the last element.
  4. using indexing, gets the second last element of v and stores the value in a variable named nextToLast. Use the index end - 1 to get the second last element.
  5. gets the number of elements in v and stores the value in a variable named len
  6. using indexing, gets the last element of v and stores the value in a variable named alsoLast. Use the index len to get the last element.