5 Write some for loops

In MATLAB open the script named labF.m.

Answer the following questions in your script.

(a) Add two vectors elementwise

The script creates two vectors x and y having the same number of random valued elements. Use a loop to add the two vectors x and y element-by-element storing the elements in the vector z.

Of course, in MATLAB you could simply write z = x + y but the purpose of this exercise is to use a for loop to perform the calculation.

(b) Compute a moving average

Many signal processing operations perform neighbourhood-based calculations. In a neighbourhood-based calculation, the result at index i is based on the values of a vector around a local neighbourhood of i. For example, suppose we have the vector v:

v = [8     4     6     2     7     3     7     7     8     5];

The moving average computes the vector avg where avg(i) is the average of the elements in the neighbourhood around v(i). Suppose that we compute the moving average using one element to the left of i, the element at i, and one element to the right of i; then avg would be equal to:

avg = [?     6     4     5     4     5.6667       5.6667     7.3333       6.6667      ?];

avg(2) is equal to mean(v(1:3)), avg(3) is equal to mean(v(2:4)), avg(4) is equal to mean(v(3:5)), and so on.

There is a problem computing avg(1) because there is no element to the left of index 1. We will choose to set avg(1) to the first moving average that we can compute; i.e., we set avg(1) to the value of avg(2) after computing the values of avg(2) through avg(9).

Similarly, there is a problem computing avg(10) because there is no element to the right of index 10. We will choose to set avg(10) to the last moving average that we can compute; i.e., we set avg(10) to the value of avg(9) after computing the values of avg(2) through avg(9).

In the script labF, show how to compute the moving average of a vector v and store the result in the vector avg. Use a for loop, indexing, and the function mean to perform the calculation.

In general, the moving average can be computed using any neighbourhood size. Later in the lab, you will be need to compute a moving average using n elements to the left and right of a value where n can be any positive integer value.

(c) Breaking out of a loop

Sometimes when using a loop you may want to stop the loop before it finishes all of its iterations. For example, suppose you have a vector x and you want to find the index of the first element in x that is less than zero. Because you want the index of the first element that is less than zero, you can stop the loop after you have found one element that is less than zero. You can exit out a loop using the break statement as shown below:

n = numel(x);
index = 0;          % the index of the first element less than zero
for i = 1:n
    if x(i) < 0
        index = i;  % remember the index of the element
        break;      % break out of the loop
    end
end
% end up here after the break statement
% index will be equal to 0 if no element of x is less than zero

In the script labF, show how to find the value lastIndex that is equal to the index of the last element in z that is less than zero. You should use a for loop and a break statement to break out of the loop. Because you want the index of the last element, your loop should count from the last index down to the first index. Note that z is almost certain to have at least one element that is negative in the problem below. In the extremely unlikely event that z does not contain a negative value, your solution should set lastIndex to 0.