function [theMode, theCount] = modecount(x) % modecount -- Mode of a sequence. % [theMode, theCount] = modecount(x) returns % the most abundant value (or values) of % vector x and its (their) count. % modecount(n) demonstrates itself for n % (default = 10) random decimal digits. %%% START USGS BOILERPLATE -------------% There is no published documentation for these programs- use the % internal comments and help function. % Programs written in Matlab- as early as 7.2.0.232 (R2006a) and % most recent modifications in v7.8.0 (R2009a) % Programs ran on PC with Windows XP Professional OS, and RHEL4 linux. % % "Although this program has been used by the USGS, no warranty, % expressed or implied, is made by the USGS or the United States % Government as to the accuracy and functioning of the program % and related program material nor shall the fact of distribution % constitute any such warranty, and no responsibility is assumed % by the USGS in connection therewith." %%% END USGS BOILERPLATE -------------- % Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 02-Apr-1998 10:50:24. if nargin < 1, help(mfilename), x = 10; end if ischar(x), x = eval(x); end if length(x) < 2 n = x; x = fix(rand(1, n)*10) [md, ct] = modecount(x) return end [m, n] = size(x); y = sort(x(:)); d = [diff(y); 1]; f = [0; find(d ~= 0)]; e = diff(f); theCount = max(e); g = find(e == theCount); f = f(g+1); result = y(f); if m == 1, result = result.'; end if nargout > 0 theMode = result; else disp(result) disp(theCount) end