How do I use the MODE function in MATLAB to find the most frequently occuring value that is closest to zero instead of the smallest?

5 views (last 30 days)
The MODE fuction will only return one value. If I execute the following code:
mode([-5 1 2 -5 3 4 1 -5 1])
It returns
ans =
-5
I would like it to return the mode value closest to zero, as opposed to the smallest value.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jan 2010
To find the mode that is closest to zero, first use the MODE function to store the smallest mode value and the number of times it occurs in the vector. Then remove those values from the vector, and use the MODE function again. If the new value returned by the MODE function occurs the same number of times as the old value, and is closer to zero, store the new value, and repeat the process. Continue removing values until you find the mode that is closest to zero.
The attached function SMALLMODE.M gives an example of how to find the most frequent value that is closest to zero using the MODE function.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!