This is a static copy of a profile report

Home

mode (Calls: 4, Time: 0.004 s)
Generated 04-Jun-2021 04:11:09 using performance time.
function in file C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\mode.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
detectTypesfunction2
detectTypes>getDominantTypesubfunction2
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
176
start = [ones(1,ncols); x(1:en...
40.000 s13.5%
95
dim = reshape(dim, 1, []);
40.000 s9.1%
184
M = reshape(M, sizem);        ...
40.000 s8.2%
84
if isnumeric(dim) && i...
40.000 s8.0%
180
freq(start) = [start(2:end); n...
40.000 s5.9%
All other lines  0.002 s55.4%
Totals  0.004 s100% 
Children (called functions)
No children
Code Analyzer results
No Code Analyzer messages.
Coverage results
Show coverage for parent directory
Total lines in function196
Non-code lines (comments, blank lines)84
Code lines (lines that can run)112
Code lines that did run47
Code lines that did not run65
Coverage (did run/can run)41.96 %
Function listing
time 
Calls 
 line
   1 
function [M,F,C] = mode(x,dim)
   2 
%MODE   Mode, or most frequent value in a sample.
   3 
%   M=MODE(X) for vector X computes M as the sample mode, or most frequently
   4 
%   occurring value in X.  For a matrix X, M is a row vector containing
   5 
%   the mode of each column.  For N-D arrays, MODE(X) is the mode of the
   6 
%   elements along the first non-singleton dimension of X.
   7 
%
   8 
%   When there are multiple values occurring equally frequently, MODE
   9 
%   returns the smallest of those values.  For complex inputs, this is taken
  10 
%   to be the first value in a sorted list of values.
  11 
%
  12 
%   [M,F]=MODE(X) also returns an array F, of the same size as M.
  13 
%   Each element of F is the number of occurrences of the corresponding
  14 
%   element of M.
  15 
%
  16 
%   [M,F,C]=MODE(X) also returns a cell array C, of the same size
  17 
%   as M.  Each element of C is a sorted vector of all the values having
  18 
%   the same frequency as the corresponding element of M.
  19 
%
  20 
%   [...]=MODE(X,'all') is the mode of all elements in X.
  21 
%
  22 
%   [...]=MODE(X,DIM) takes the mode along the dimension DIM of X.
  23 
%
  24 
%   [...]=MODE(X,VECDIM) operates on the dimensions specified in the vector 
  25 
%   VECDIM. For example, MODE(X,[1 2]) operates on the elements contained
  26 
%   in the first and second dimensions of X.
  27 
%
  28 
%   This function is most useful with discrete or coarsely rounded data.
  29 
%   The mode for a continuous probability distribution is defined as
  30 
%   the peak of its density function.  Applying the MODE function to a
  31 
%   sample from that distribution is unlikely to provide a good estimate
  32 
%   of the peak; it would be better to compute a histogram or density
  33 
%   estimate and calculate the peak of that estimate.  Also, the MODE
  34 
%   function is not suitable for finding peaks in distributions having
  35 
%   multiple modes.
  36 
%
  37 
%   Example:
  38 
%       X = [3 3 1 4; 0 0 1 1; 0 1 2 4]
  39 
%       mode(X,1)
  40 
%       mode(X,2)
  41 
%
  42 
%      % To find the mode of a continuous variable grouped into bins:
  43 
%      y = randn(1000,1);
  44 
%      edges = -6:.25:6;
  45 
%      bin = discretize(y,edges);
  46 
%      m = mode(bin);
  47 
%      edges([m, m+1])
  48 
%      histogram(y,edges)
  49 
%
  50 
%   Class support for input X:
  51 
%      float:  double, single
  52 
%      integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
  53 
%
  54 
%   See also MEAN, MEDIAN, HISTOGRAM, HISTCOUNTS.
  55 

  56 
%   Copyright 2005-2019 The MathWorks, Inc.
  57 

< 0.001 
      4 
  58
if isstring(x) 
  59 
    error(message('MATLAB:mode:wrongInput'));
< 0.001 
      4 
  60
end 
  61 

< 0.001 
      4 
  62
dofreq = nargout>=2; 
< 0.001 
      4 
  63
docell = nargout>=3; 
  64 

< 0.001 
      4 
  65
if nargin<2 
  66 
    % Special case to make mode, mean, and median behave similarly for []
  67 
    if isequal(x, [])
  68 
        M = modeForEmpty(x,1);
  69 
        if dofreq
  70 
            F = zeros(1,1,'like',full(double(x([]))));
  71 
        end
  72 
        if docell
  73 
            C = {zeros(0,1,'like',x)};
  74 
        end
  75 
        return
  76 
    end
  77 
    
  78 
    % Determine the first non-singleton dimension
  79 
    dim = find(size(x)~=1, 1);
  80 
    if isempty(dim)
  81 
        dim = 1;
  82 
    end
< 0.001 
      4 
  83
else 
< 0.001 
      4 
  84
    if isnumeric(dim) && isvector(dim) 
< 0.001 
      4 
  85
        if isempty(dim) 
  86 
            error(message('MATLAB:mode:BadDim'));
< 0.001 
      4 
  87
        else 
< 0.001 
      4 
  88
            if ~isreal(dim) || any(dim~=floor(dim)) || any(dim<1) || any(~isfinite(dim)) 
  89 
                error(message('MATLAB:mode:BadDim'));
< 0.001 
      4 
  90
            end 
< 0.001 
      4 
  91
            if ~isscalar(dim) && ~all(diff(sort(dim))) 
  92 
                error(message('MATLAB:mode:vecDimsMustBeUniquePositiveIntegers'));
< 0.001 
      4 
  93
            end 
< 0.001 
      4 
  94
        end 
< 0.001 
      4 
  95
        dim = reshape(dim, 1, []); 
  96 
    elseif (ischar(dim) && isrow(dim)) || (isstring(dim) && isscalar(dim) && strlength(dim) > 0)
  97 
        if strncmpi(dim,'all',max(strlength(dim), 1))
  98 
            x = x(:);
  99 
            dim = 1;
 100 
        else
 101 
            error(message('MATLAB:mode:BadDim'));
 102 
        end
 103 
    else
 104 
        error(message('MATLAB:mode:BadDim'));
< 0.001 
      4 
 105
    end 
< 0.001 
      4 
 106
end 
 107 

< 0.001 
      4 
 108
dim = min(dim, ndims(x)+1); 
 109 

< 0.001 
      4 
 110
sizex = size(x); 
< 0.001 
      4 
 111
if max(dim)>length(sizex) 
 112 
    sizex(end+1:max(dim)) = 1;
< 0.001 
      4 
 113
end 
 114 

< 0.001 
      4 
 115
sizem = sizex; 
< 0.001 
      4 
 116
sizem(dim) = 1; 
 117 

< 0.001 
      4 
 118
tf = false(size(sizem)); 
< 0.001 
      4 
 119
tf(dim) = true; 
< 0.001 
      4 
 120
dim = find(tf); 
 121 

 122 
% Dispose of empty arrays right away
< 0.001 
      4 
 123
if isempty(x) 
 124 
    M = modeForEmpty(x,sizem);
 125 
    if dofreq
 126 
        F = zeros(sizem,'like',full(double(x([]))));
 127 
    end
 128 
    if docell
 129 
        C = cell(sizem);
 130 
        C(:) = {M(1:0)};  % fill C with empties of the proper type
 131 
    end
 132 
    return
< 0.001 
      4 
 133
end 
 134 

< 0.001 
      4 
 135
if isvector(x) && (isscalar(dim) && dim <=2) 
 136 
    % Treat vectors separately
 137 
    if (iscolumn(x) && dim == 2) || (~iscolumn(x) && dim == 1)
 138 
        % No computation needed for mode(col,2) and mode(row,1)
 139 
        M = x;
 140 
        
 141 
        if dofreq
 142 
            F = ones(sizex,'like',full(double(x([]))));
 143 
        end
 144 
        if docell
 145 
            C = num2cell(x);
 146 
        end
 147 
    else
 148 
        % Sort the vector and compute the mode
 149 
        x = sort(x(:));
 150 
        % start of run of equal values
 151 
        start = find([1; x(1:end-1)~=x(2:end)]);
 152 
        % frequencies for each run (force to double datatype)
 153 
        freq = zeros(numel(x),1,'like',full(double(x([]))));
 154 
        freq(start) = [diff(start); numel(x)+1-start(end)];
 155 
        [maxfreq,firstloc] = max(freq);
 156 
        
 157 
        M = x(firstloc);                % Mode
 158 
        
 159 
        if dofreq
 160 
            F = maxfreq;                % Frequency
 161 
        end
 162 
        if docell
 163 
            C = {x(freq == maxfreq)};   % Cell array with modes
 164 
        end
 165 
    end
< 0.001 
      4 
 166
else 
 167 
    % Permute data and reshape into a 2-D array
< 0.001 
      4 
 168
    perm = [dim, find(~tf)]; 
< 0.001 
      4 
 169
    x = permute(x, perm); 
< 0.001 
      4 
 170
    x = reshape(x, [prod(sizex(dim)), prod(sizem)]); 
< 0.001 
      4 
 171
    [nrows,ncols] = size(x); 
 172 
    
 173 
    % Compute the modes for each column of the 2-D array
< 0.001 
      4 
 174
    x = sort(x,1); 
 175 
    % start of run of equal values
< 0.001 
      4 
 176
    start = [ones(1,ncols); x(1:end-1,:)~=x(2:end,:)]; 
< 0.001 
      4 
 177
    start = find(start(:)); 
 178 
    % frequencies for each run (force to double datatype)
< 0.001 
      4 
 179
    freq = zeros([nrows,ncols],'like',full(double(x([])))); 
< 0.001 
      4 
 180
    freq(start) = [start(2:end); numel(x)+1]-start; 
< 0.001 
      4 
 181
    [maxfreq,firstloc] = max(freq,[],1); 
 182 
    
< 0.001 
      4 
 183
    M = x((0:nrows:numel(x)-1)+firstloc);           % Modes for each column 
< 0.001 
      4 
 184
    M = reshape(M, sizem);           % Reshape and permute back 
 185 
    
< 0.001 
      4 
 186
    if dofreq 
 187 
        F = reshape(maxfreq, sizem); % Frequencies
< 0.001 
      4 
 188
    end 
< 0.001 
      4 
 189
    if docell 
 190 
        C = cell(size(M));                          % Cell array with modes
 191 
        selection = freq == maxfreq;
 192 
        for j = 1:numel(M)
 193 
            C{j} = x(selection(:,j),j);
 194 
        end
< 0.001 
      4 
 195
    end 
< 0.001 
      4 
 196
end 

Other subfunctions in this file are not included in this listing.