12 Use indexing to select multiple elements from a vector

In MATLAB, a programmer can use a vector of indices to select multiple elements from a vector or array. For example, to get the first and last elements of a vector x we can write:

x = [9 8 7 6];
ind = [1 numel(x)];    % first and last indices of x
y = x(ind)

Multiple elements of a vector can be changed as well; for example to change the first and last elements of a vector x we can write:

x = [9 8 7 6];
ind = [1 numel(x)];    % first and last indices of x
x(ind) = [-100 100]

Create a new script and save it as q7.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. Gets the first 3 elements of v and stores them in a variable named first3
  2. Gets the last 3 elements of v and stores them in a variable named last3
  3. Gets every other element of v starting at the first element and stores them in a variable named everyOther
  4. Gets every other element of v starting at the second element and stores them in a variable named everyOther2