No BSD License
Highlights from
Modal
from
Modal
by Felix Hebeler
Calculates statistical mode and returns indices
|
| modal(A)
|
function [m,n,in] = modal(A)
% function to find the most occuring element (modus/modal)
% in a vector
% Usage: [m,in] = modal(A)
% where A is the array to analyse
% Returns element m (modal), n (no of occurrences) and in (indices of m) in
% array A.
% Note: For matrices, use modal(A(:))
% Example: [m,n,in]=modal(round(rand(20,1)*10))
% initialise output
m=nan;
n=0; % holds the occurances of element m
A(isnan(A))=[];
i=1;
while i<=length(A) % loop over vector
[s,l]=find(A==A(i)); % find first element
s=sum(s); % get occurances
if s>n % if it occurs more often than previous store
n=s; % the number of occurances
m=A(i); % the element that occured
in=l; % and the indices where it occured
end
A(A==A(i))=[]; % delete element occurances we just examine so its not counted again
i=i+1; % and proceed with the next element
end
|
|
Contact us at files@mathworks.com