8 Use indexing to set a single element of a vector
Create a new script and save it as q3.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, sets the first element of
v
to the value -999 - using indexing, sets the second element of
v
to the valuepi
- using indexing, sets the last element of
v
999 - using indexing, sets the middle element of
v
to 0. The middle element ofv
has the index equal to the number of elements inv
divided by 2 rounded up to the nearest integer.