Given a vector, we have discussed the basic algorithm for fiding the
maximum, by keep track of the maximum thus far while iterating through
the vector.
Question: how many different elements qualify as being the
maximum thus far during that process?
Goal: Develop a function peaks(v) that returns the number of
elements that have value surpassing all previous elements in the vector.
For example, peaks([5 1 8 3 8 11 10]) should return
3, as the first element, the third element and the sixth
elements are new maximums (we do not count the fifth, because that
value 8 did not surpass the previously visited maximum of 8).
Goal: Rewrite the function so that it can provide a second return
value, which is a vector of indices achieving those peaks (akin to the
second return value of the built-in max function). Thus
peaks([5 1 8 3 8 11 10]) should return the pair
[ 3 [1 3 6] ].
Files from Wednesday's class
We developed three function m-files during class. Because these were
written on-the-fly, they may not be perfect examples of good style,
but the should suffice.
-
peaks.m
The basic function for computing the number of peaks and their indices.
-
harmonicTest.m
A function that performed tests by generating random vectors and
counting the number of peaks in those vectors.
-
graphHarmonic.m
A function that produces a plot of the theoretical predicted
number of peaks for random data versus the observed data for
random trials.
Last modified: Wednesday, 18 February 2009