This is a static copy of a profile report

Home

Function details for uniqueThis is a static copy of a profile report

Home

unique (Calls: 25, Time: 0.019 s)
Generated 28-Jun-2020 10:10:20 using performance time.
function in file /Applications/MATLAB_R2018a.app/toolbox/matlab/ops/unique.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ismember>ismemberClassTypessubfunction20
setdiff>setdiffR2012asubfunction5
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
155
[varargout{1:nlhs}] = uniqueR2...
250.013 s70.0%
128
end
250.001 s2.6%
148
if flaginds(4) || flaginds(5) ...
250.000 s2.5%
134
if flaginds(4) && flag...
250.000 s2.1%
89
for i = 2:nargin
250.000 s1.8%
All other lines  0.004 s20.9%
Totals  0.019 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
unique>uniqueR2012asubfunction250.011 s56.8%
Self time (built-ins, overhead, etc.)  0.008 s43.2%
Totals  0.019 s100% 
Code Analyzer results
Line numberMessage
Coverage results
Show coverage for parent directory
Total lines in function164
Non-code lines (comments, blank lines)92
Code lines (lines that can run)72
Code lines that did run37
Code lines that did not run35
Coverage (did run/can run)51.39 %
Function listing
time 
Calls 
 line
   1 
function varargout = unique(varargin) 
   2 
%UNIQUE Set unique.
   3 
%   C = UNIQUE(A) for the array A returns the same values as in A but with 
   4 
%   no repetitions. C will be sorted.    
   5 
%  
   6 
%   C = UNIQUE(A,'rows') for the matrix A returns the unique rows of A.
   7 
%   The rows of the matrix C will be in sorted order.
   8 
%  
   9 
%   [C,IA,IC] = UNIQUE(A) also returns index vectors IA and IC such that
  10 
%   C = A(IA) and A = C(IC) (or A(:) = C(IC), if A is a matrix or array).  
  11 
%  
  12 
%   [C,IA,IC] = UNIQUE(A,'rows') also returns index vectors IA and IC such
  13 
%   that C = A(IA,:) and A = C(IC,:). 
  14 
%  
  15 
%   [C,IA,IC] = UNIQUE(A,OCCURRENCE) and
  16 
%   [C,IA,IC] = UNIQUE(A,'rows',OCCURRENCE) specify which index is returned
  17 
%   in IA in the case of repeated values (or rows) in A. The default value
  18 
%   is OCCURENCE = 'first', which returns the index of the first occurrence 
  19 
%   of each repeated value (or row) in A, while OCCURRENCE = 'last' returns
  20 
%   the index of the last occurrence of each repeated value (or row) in A.
  21 
%  
  22 
%   [C,IA,IC] = UNIQUE(A,'stable') returns the values of C in the same order
  23 
%   that they appear in A, while [C,IA,IC] = UNIQUE(A,'sorted') returns the
  24 
%   values of C in sorted order. If A is a row vector, then C will be a row
  25 
%   vector as well, otherwise C will be a column vector. IA and IC are
  26 
%   column vectors. If there are repeated values in A, then IA returns the
  27 
%   index of the first occurrence of each repeated value.
  28 
% 
  29 
%   [C,IA,IC] = UNIQUE(A,'rows','stable') returns the rows of C in the same
  30 
%   order that they appear in A, while [C,IA,IC] = UNIQUE(A,'rows','sorted')
  31 
%   returns the rows of C in sorted order.
  32 
% 
  33 
%   The behavior of UNIQUE has changed.  This includes:
  34 
%     -	occurrence of indices in IA and IC switched from last to first
  35 
%     -	IA and IC will always be column index vectors
  36 
% 
  37 
%   If this change in behavior has adversely affected your code, you may 
  38 
%   preserve the previous behavior with:
  39 
% 
  40 
%        [C,IA,IC] = UNIQUE(A,'legacy')
  41 
%        [C,IA,IC] = UNIQUE(A,'rows','legacy') 
  42 
%        [C,IA,IC] = UNIQUE(A,OCCURRENCE,'legacy')
  43 
%        [C,IA,IC] = UNIQUE(A,'rows',OCCURRENCE,'legacy')
  44 
%
  45 
%   Examples:
  46 
%
  47 
%       a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2 1]
  48 
%
  49 
%       [c1,ia1,ic1] = unique(a)
  50 
%       % returns
  51 
%       c1 = [1 2 4 5 6 7 8 9]
  52 
%       ia1 = [21 20 19 17 14 11 7 1]'
  53 
%       ic1 = [8 8 8 8 8 8 7 7 7 7 6 6 6 5 5 5 4 4 3 2 1]'
  54 
%
  55 
%       [c2,ia2,ic2] = unique(a,'stable')
  56 
%       % returns
  57 
%       c2 = [9 8 7 6 5 4 2 1]
  58 
%       ia2 = [1 7 11 14 17 19 20 21]'
  59 
%       ic2 = [1 1 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 6 7 8]'
  60 
%
  61 
%       c = unique([1 NaN NaN 2])
  62 
%       % NaNs compare as not equal, so this returns
  63 
%       c = [1 2 NaN NaN]
  64 
%
  65 
%   Class support for input A:
  66 
%      - logical, char, all numeric classes
  67 
%      - cell arrays of strings
  68 
%      -- 'rows' option is not supported for cell arrays
  69 
%      - objects with methods SORT (SORTROWS for the 'rows' option) and NE
  70 
%      -- including heterogeneous arrays
  71 
%
  72 
%   See also UNIQUETOL, INTERSECT, ISMEMBER, ISMEMBERTOL, UNION, SETDIFF,
  73 
%            SETXOR, SORT, SORTROWS.
  74 

  75 
%   Copyright 1984-2017 The MathWorks, Inc.
  76 

  77 
% Determine the number of outputs requested.
  78 

< 0.001 
     25 
  79
if nargout == 0 
  80 
    nlhs = 1;
< 0.001 
     25 
  81
else 
< 0.001 
     25 
  82
    nlhs = nargout; 
< 0.001 
     25 
  83
end 
  84 

< 0.001 
     25 
  85
narginchk(1,4); 
  86 
% Convert string flags to char flags to dispatch to the right method
< 0.001 
     25 
  87
if nargin > 1 
< 0.001 
     25 
  88
    hadStringArguments = false; 
< 0.001 
     25 
  89
    for i = 2:nargin 
< 0.001 
     25 
  90
        if isstring(varargin{i}) 
  91 
            varargin{i} = convertFlag(varargin{i});
  92 
            hadStringArguments = true;
  93 
        end
< 0.001 
     25 
  94
    end 
< 0.001 
     25 
  95
    if hadStringArguments 
  96 
        [varargout{1:nlhs}] = unique(varargin{:});
  97 
        return;
  98 
    end
< 0.001 
     25 
  99
end 
 100 

< 0.001 
     25 
 101
nrhs = nargin; 
< 0.001 
     25 
 102
if nrhs == 1 
 103 
    [varargout{1:nlhs}] = uniqueR2012a(varargin{:});
< 0.001 
     25 
 104
else 
 105 
    % acceptable combinations, with optional inputs denoted in []
 106 
    % unique(A, ['rows'], ['first'/'last'], ['legacy'/'R2012a']),
 107 
    % where the position of 'rows' and 'first'/'last' may be reversed
 108 
    % unique(A, ['rows'], ['sorted'/'stable']),
 109 
    % where the position of 'rows' and 'sorted'/'stable' may be reversed
< 0.001 
     25 
 110
    flagvals = ["rows" "first" "last" "sorted" "stable" "legacy" "R2012a"]; 
 111 
    % When a flag is found, note the index into varargin where it was found
< 0.001 
     25 
 112
    flaginds = zeros(1,length(flagvals)); 
< 0.001 
     25 
 113
    for i = 2:nrhs 
< 0.001 
     25 
 114
        flag = varargin{i}; 
< 0.001 
     25 
 115
        assert(~isstring(flag)) 
< 0.001 
     25 
 116
        if ~ischar(flag) 
 117 
            error(message('MATLAB:UNIQUE:UnknownInput'));
 118 
        end
< 0.001 
     25 
 119
        foundflag = startsWith(flagvals,flag,'IgnoreCase',true); 
< 0.001 
     25 
 120
        if sum(foundflag) ~= 1 
 121 
            error(message('MATLAB:UNIQUE:UnknownFlag',flag));
 122 
        end
 123 
        % Only 1 occurrence of each allowed flag value
< 0.001 
     25 
 124
        if flaginds(foundflag) 
 125 
            error(message('MATLAB:UNIQUE:RepeatedFlag',flag));
 126 
        end
< 0.001 
     25 
 127
        flaginds(foundflag) = i; 
< 0.001 
     25 
 128
    end 
 129 
    
 130 
    % Only 1 of each of the paired flags
< 0.001 
     25 
 131
    if flaginds(2) && flaginds(3) 
 132 
        error(message('MATLAB:UNIQUE:OccurrenceConflict'))
 133 
    end
< 0.001 
     25 
 134
    if flaginds(4) && flaginds(5) 
 135 
        error(message('MATLAB:UNIQUE:SetOrderConflict'))
 136 
    end
< 0.001 
     25 
 137
    if flaginds(6) && flaginds(7) 
 138 
        error(message('MATLAB:UNIQUE:BehaviorConflict'))
 139 
    end
 140 
    % 'legacy' and 'R2012a' flags must be trailing
< 0.001 
     25 
 141
    if flaginds(6) && flaginds(6)~=nrhs 
 142 
        error(message('MATLAB:UNIQUE:LegacyTrailing'))
 143 
    end
< 0.001 
     25 
 144
    if flaginds(7) && flaginds(7)~=nrhs 
 145 
        error(message('MATLAB:UNIQUE:R2012aTrailing'))
 146 
    end
 147 
    
< 0.001 
     25 
 148
    if flaginds(4) || flaginds(5) % 'stable'/'sorted' specified 
< 0.001 
     25 
 149
        if flaginds(2) || flaginds(3) % does not combine with 'first'/'last' 
 150 
            error(message('MATLAB:UNIQUE:SetOrderOccurrence'))
 151 
        end
< 0.001 
     25 
 152
        if flaginds(6) || flaginds(7) % does not combine with 'legacy'/'R2012a' 
 153 
            error(message('MATLAB:UNIQUE:SetOrderBehavior'))
 154 
        end
  0.013 
     25 
 155
        [varargout{1:nlhs}] = uniqueR2012a(varargin{1},logical(flaginds(1:5))); 
 156 
    elseif flaginds(7) % trailing 'R2012a' specified
 157 
        [varargout{1:nlhs}] = uniqueR2012a(varargin{1},logical(flaginds(1:5)));
 158 
    elseif flaginds(6) % trailing 'legacy' specified
 159 
        [varargout{1:nlhs}] = uniquelegacy(varargin{1},logical(flaginds(1:3)));
 160 
    else % 'R2012a' (default behavior, to be changed to 'R2012a' in future)
 161 
        [varargout{1:nlhs}] = uniqueR2012a(varargin{1},logical(flaginds(1:5)));
 162 
    end
< 0.001 
     25 
 163
end 
< 0.001 
     25 
 164
end 

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