SUNY Geneseo Department of Mathematics

Lab 5 Extra Credit—Decisions without If

Math 230 02
Spring 2017
Prof. Doug Baldwin

Complete by Wednesday, March 8
Grade by Tuesday, March 21

Purpose

This is an extra credit extension to Lab 5 on making decisions. This exercise extends the original lab by giving you practice with ways of making decisions via comparison operations, logic, and arithmetic on vectors instead of via if statements.

Background

This exercise requires fairly clever use of multi-element vector indexing, which is covered in section 4.8 of our textbook. We used multi-element indexing on a sound signal to hear the consequences of Nyquist’s theorem in class on February 8.

In an indexing array, the special Matlab variable end stands for the largest legal index into the indexed array. You may find this helpful in doing this exercise. You can read about end in section 4.10 of our textbook.

There is also a useful consequence of the bracket notation for constructing vectors: if one of the values inside the brackets is another vector, that entire vector is inserted into the vector being constructed. For example, the expression

[ 3, [5:10] ]

creates the vector [ 3, 5, 6, 7, 8, 9, 10 ].

Finally, you may find it useful to know that Matlab has a built-in variable inf that represents an infinitely large number, i.e., a number larger than any other number Matlab can represent.

Activity

Back in Lab 2, you had to count how many peaks there were in a vector that represented a wave. In that lab, most of the work was done for you by built-in function findPeaks. Your job in this extension is to write a peak-counter without using findPeaks or any similar built-in function. You also may not use “if” or similar statements.

Specifically, write a function countPeaks that takes a vector as its argument and that returns the number of positions in that vector where a value is greater than both the immediately preceding value and the immediately succeeding value. Do this by creating relevant vectors, using comparison and logical operations on them, and adding together all the 1s in a resulting logical vector (recall that the sum function will do this). Note that the definition of a “peak” as a place where a value is greater than both the preceding and succeeding values means that the first and last elements of the vector are never peaks, since they lack either a preceding or a succeeding value.

The previous paragraph gives you some hints on how you might solve this problem, although it also leaves some things for you to discover for yourself. All of the Matlab features mentioned in the “Background” also appeared in my solution to this exercise.

Follow-Up

I will grade this extension at the same time I grade the rest of your Lab 5. Bring your solution to the extension to that meeting along with your solution to the rest of the lab.

This extension is worth up to 2 points extra credit on the lab.