9 Use indexing to delete an element of a vector
In MATLAB, the empty array is created using the syntax []
. For example, we can make a variable x
equal to the empty array by typing:
x = [];
Setting an element of an array to the empty array removes the element from the array. For example, to remove the 8 from the array x
we could write:
x = [9 8 7];
x(2) = []
Create a new script and save it as q4.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:
- removes the first element of
v
- makes a new variable
w
that is equal tov
- removes the first element of
w
(but does not modifyv
) - makes a new variable
x
that is equal tow
- removes the last element of
x