This is a static copy of a profile report

Home

mode (2 calls, 0.020 sec)
Generated 14-Nov-2016 07:47:12 using cpu time.
function in file /usr/local/MATLAB/MATLAB_Production_Server/R2015a/toolbox/matlab/datafun/mode.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
FindChrSizes_4function1
allelic_ratios_WGseqfunction1
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
132
[maxfreq,firstloc] = max(freq)...
20.010 s50.0%
128
start = find([1; x(1:end-1)~=x...
20.010 s50.0%
142
end
20 s0%
139
if docell
20 s0%
136
if dofreq
20 s0%
All other lines  0 s0%
Totals  0.020 s100% 
Children (called functions)
No children
Code Analyzer results
No Code Analyzer messages.
Coverage results
Show coverage for parent directory
Total lines in function174
Non-code lines (comments, blank lines)79
Code lines (lines that can run)95
Code lines that did run23
Code lines that did not run72
Coverage (did run/can run)24.21 %
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,DIM) takes the mode along the dimension DIM of X.
  21 
%
  22 
%   This function is most useful with discrete or coarsely rounded data.
  23 
%   The mode for a continuous probability distribution is defined as
  24 
%   the peak of its density function.  Applying the MODE function to a
  25 
%   sample from that distribution is unlikely to provide a good estimate
  26 
%   of the peak; it would be better to compute a histogram or density
  27 
%   estimate and calculate the peak of that estimate.  Also, the MODE
  28 
%   function is not suitable for finding peaks in distributions having
  29 
%   multiple modes.
  30 
%
  31 
%   Example: If X = [3 3 1 4
  32 
%                    0 0 1 1
  33 
%                    0 1 2 4]
  34 
%
  35 
%   then mode(X) is [0 0 1 4] and mode(X,2) is [3
  36 
%                                               0
  37 
%                                               0]
  38 
%
  39 
%   To find the mode of a continuous variable grouped into bins:
  40 
%      y = randn(1000,1);
  41 
%      edges = -6:.25:6;
  42 
%      [n,bin] = histc(y,edges);
  43 
%      m = mode(bin);
  44 
%      edges([m, m+1])
  45 
%      hist(y,edges+.125)
  46 
%
  47 
%   Class support for input X:
  48 
%      float:  double, single
  49 
%      integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
  50 
%
  51 
%   See also MEAN, MEDIAN, HISTOGRAM, HISTCOUNTS.
  52 

  53 
%   Copyright 2005-2014 The MathWorks, Inc.
  54 

      2 
  55 
dofreq = nargout>=2; 
      2 
  56 
docell = nargout>=3; 
  57 

      2 
  58 
if nargin<2 
  59 
    % Special case to make mode and mean behave similarly
      2 
  60 
    if isequal(x, []) 
  61 
        if isinteger(x)
  62 
            M = zeros('like',x);
  63 
        else
  64 
            M = NaN('like',x);
  65 
        end
  66 
        if dofreq
  67 
            F = zeros(1,1,'like',full(double(x([]))));
  68 
        end
  69 
        if docell
  70 
            C = {zeros(0,1,'like',x)};
  71 
        end
  72 
        warning(message('MATLAB:mode:EmptyInput'))
  73 
        return
  74 
    end
  75 
    
  76 
    % Determine the first non-singleton dimension
      2 
  77 
    dim = find(size(x)~=1, 1); 
      2 
  78 
    if isempty(dim) 
  79 
        dim = 1;
  80 
    end
  81 
else
  82 
    if ~isscalar(dim)   || ~isnumeric(dim)  || dim~=floor(dim)  || ...
  83 
            dim<1       || ~isreal(dim)     || ~isfinite(dim)
  84 
        error(message('MATLAB:mode:BadDim'));
  85 
    end
  86 
end
  87 

      2 
  88 
sizex = size(x); 
      2 
  89 
if dim>length(sizex) 
  90 
    sizex(length(sizex)+1:dim) = 1;
  91 
end
  92 

      2 
  93 
sizem = sizex; 
      2 
  94 
sizem(dim) = 1; 
  95 

  96 
% Dispose of empty arrays right away
      2 
  97 
if isempty(x) 
  98 
    M = zeros(sizem,'like',x);
  99 
    if prod(sizem)>0
 100 
        M(:) = NaN;
 101 
    end
 102 
    if dofreq
 103 
        F = zeros(sizem,'like',full(double(x([]))));
 104 
    end
 105 
    if docell
 106 
        C = cell(sizem);
 107 
        C(:) = {M(1:0)};  % fill C with empties of the proper type
 108 
    end
 109 
    return
 110 
end
 111 

      2 
 112 
if isvector(x) && dim <=2 
 113 
    % Treat vectors separately
      2 
 114 
    if (iscolumn(x) && dim == 2) || (~iscolumn(x) && dim == 1) 
 115 
        % No computation needed for mode(col,2) and mode(row,1)
 116 
        M = x;
 117 
        
 118 
        if dofreq
 119 
            F = ones(sizex,'like',full(double(x([]))));
 120 
        end
 121 
        if docell
 122 
            C = num2cell(x);
 123 
        end
      2 
 124 
    else 
 125 
        % Sort the vector and compute the mode
      2 
 126 
        x = sort(x(:)); 
 127 
        % start of run of equal values
  0.01 
      2 
 128 
        start = find([1; x(1:end-1)~=x(2:end)]); 
 129 
        % frequencies for each run (force to double datatype)
      2 
 130 
        freq = zeros(numel(x),1,'like',full(double(x([])))); 
      2 
 131 
        freq(start) = [diff(start); numel(x)+1-start(end)]; 
  0.01 
      2 
 132 
        [maxfreq,firstloc] = max(freq); 
 133 
        
      2 
 134 
        M = x(firstloc);                % Mode 
 135 
        
      2 
 136 
        if dofreq 
 137 
            F = maxfreq;                % Frequency
 138 
        end
      2 
 139 
        if docell 
 140 
            C = {x(freq == maxfreq)};   % Cell array with modes
 141 
        end
      2 
 142 
    end 
 143 
else
 144 
    % Permute data and reshape into a 2-D array
 145 
    perm = [dim, (1:dim-1), (dim+1:length(sizex))];
 146 
    sizem = sizem(perm);
 147 
    x = permute(x, perm);
 148 
    x = reshape(x,[sizex(dim),prod(sizem)]);
 149 
    [nrows,ncols] = size(x);
 150 
    
 151 
    % Compute the modes for each column of the 2-D array
 152 
    x = sort(x,1);
 153 
    % start of run of equal values
 154 
    start = [ones(1,ncols); x(1:end-1,:)~=x(2:end,:)];
 155 
    start = find(start(:));
 156 
    % frequencies for each run (force to double datatype)
 157 
    freq = zeros([nrows,ncols],'like',full(double(x([]))));
 158 
    freq(start) = [start(2:end); numel(x)+1]-start;
 159 
    [maxfreq,firstloc] = max(freq,[],1);
 160 
    
 161 
    M = x((0:nrows:numel(x)-1)+firstloc);           % Modes for each column
 162 
    M = ipermute(reshape(M,sizem), perm);           % Reshape and permute back
 163 
    
 164 
    if dofreq
 165 
        F = ipermute(reshape(maxfreq,sizem), perm); % Frequencies
 166 
    end
 167 
    if docell
 168 
        C = cell(size(M));                          % Cell array with modes
 169 
        selection = bsxfun(@eq, freq, maxfreq);
 170 
        for j = 1:numel(M)
 171 
            C{j} = x(selection(:,j),j);
 172 
        end
 173 
    end
 174 
end