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 OCCURRENCE = '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-2018 The MathWorks, Inc.
|
| | 76 |
|
| | 77 | % Determine the number of outputs requested.
|
| | 78 |
|
0.003 | 44478 | 79 | if nargout == 0
|
| | 80 | nlhs = 1;
|
0.003 | 44478 | 81 | else
|
0.004 | 44478 | 82 | nlhs = nargout;
|
0.002 | 44478 | 83 | end
|
| | 84 |
|
0.003 | 44478 | 85 | narginchk(1,4);
|
| | 86 | % Convert string flags to char flags to dispatch to the right method
|
0.003 | 44478 | 87 | if nargin > 1
|
| | 88 | hadStringArguments = false;
|
| | 89 | for i = 2:nargin
|
| | 90 | if isstring(varargin{i})
|
| | 91 | varargin{i} = convertFlag(varargin{i});
|
| | 92 | hadStringArguments = true;
|
| | 93 | end
|
| | 94 | end
|
| | 95 | if hadStringArguments
|
| | 96 | [varargout{1:nlhs}] = unique(varargin{:});
|
| | 97 | return;
|
| | 98 | end
|
| | 99 | end
|
| | 100 |
|
0.002 | 44478 | 101 | nrhs = nargin;
|
0.003 | 44478 | 102 | if nrhs == 1
|
0.955 | 44478 | 103 | [varargout{1:nlhs}] = uniqueR2012a(varargin{:});
|
| | 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
|
| | 110 | flagvals = ["rows" "first" "last" "sorted" "stable" "legacy" "R2012a"];
|
| | 111 | % When a flag is found, note the index into varargin where it was found
|
| | 112 | flaginds = zeros(1,length(flagvals));
|
| | 113 | for i = 2:nrhs
|
| | 114 | flag = varargin{i};
|
| | 115 | assert(~isstring(flag))
|
| | 116 | if ~ischar(flag)
|
| | 117 | error(message('MATLAB:UNIQUE:UnknownInput'));
|
| | 118 | end
|
| | 119 | foundflag = startsWith(flagvals,flag,'IgnoreCase',true);
|
| | 120 | if sum(foundflag) ~= 1
|
| | 121 | error(message('MATLAB:UNIQUE:UnknownFlag',flag));
|
| | 122 | end
|
| | 123 | % Only 1 occurrence of each allowed flag value
|
| | 124 | if flaginds(foundflag)
|
| | 125 | error(message('MATLAB:UNIQUE:RepeatedFlag',flag));
|
| | 126 | end
|
| | 127 | flaginds(foundflag) = i;
|
| | 128 | end
|
| | 129 |
|
| | 130 | % Only 1 of each of the paired flags
|
| | 131 | if flaginds(2) && flaginds(3)
|
| | 132 | error(message('MATLAB:UNIQUE:OccurrenceConflict'))
|
| | 133 | end
|
| | 134 | if flaginds(4) && flaginds(5)
|
| | 135 | error(message('MATLAB:UNIQUE:SetOrderConflict'))
|
| | 136 | end
|
| | 137 | if flaginds(6) && flaginds(7)
|
| | 138 | error(message('MATLAB:UNIQUE:BehaviorConflict'))
|
| | 139 | end
|
| | 140 | % 'legacy' and 'R2012a' flags must be trailing
|
| | 141 | if flaginds(6) && flaginds(6)~=nrhs
|
| | 142 | error(message('MATLAB:UNIQUE:LegacyTrailing'))
|
| | 143 | end
|
| | 144 | if flaginds(7) && flaginds(7)~=nrhs
|
| | 145 | error(message('MATLAB:UNIQUE:R2012aTrailing'))
|
| | 146 | end
|
| | 147 |
|
| | 148 | if flaginds(4) || flaginds(5) % 'stable'/'sorted' specified
|
| | 149 | if flaginds(2) || flaginds(3) % does not combine with 'first'/'last'
|
| | 150 | error(message('MATLAB:UNIQUE:SetOrderOccurrence'))
|
| | 151 | end
|
| | 152 | if flaginds(6) || flaginds(7) % does not combine with 'legacy'/'R2012a'
|
| | 153 | error(message('MATLAB:UNIQUE:SetOrderBehavior'))
|
| | 154 | end
|
| | 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.002 | 44478 | 163 | end
|
0.023 | 44478 | 164 | end
|
Other subfunctions in this file are not included in this listing.