Question
MATLAB provides a function mode that computes the mode of a set of values, that is, the single most frequently occuring value (in the case
MATLAB provides a function mode that computes the mode of a set of values, that is, the single most frequently occuring value (in the case of a tie, it returns the smallest value among those having the maximum frequency). For example, the command m = mode([5 8 2 9 2 5 7 2 6 5 4]) sets variable m to the value 2, as it and 5 occurs three times each, with 2 chosen based on the tie-breaking rule. There is a second form of the command [m n] = mode(V) where m is the value of the mode and n is its frequency. For example, the command [m n] = mode([5 8 2 9 2 5 7 2 6 5 4]) sets m = 2 and n = 3.
Your task is to implement a function called myMode(v) that has similar behavior when called upon a vector of values. You are not allowed to use the official mode function. However, you may use the built-in function sort which sorts values of a vector into non-decreasing order. Once the data is sorted, finding the mode is equivalent to looking for the longest consecutive streak of equal values.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started