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).
|
| | 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 UNION, INTERSECT, SETDIFF, SETXOR, ISMEMBER, SORT, SORTROWS.
|
| | 73 |
|
| | 74 | % Copyright 1984-2018 The MathWorks, Inc.
|
| | 75 |
|
| | 76 | % Determine the number of outputs requested.
|
< 0.001 | 20 | 77 | if nargout == 0
|
| | 78 | nlhs = 1;
|
< 0.001 | 20 | 79 | else
|
< 0.001 | 20 | 80 | nlhs = nargout;
|
< 0.001 | 20 | 81 | end
|
| | 82 |
|
< 0.001 | 20 | 83 | narginchk(1,4);
|
< 0.001 | 20 | 84 | if ~iscellstr(varargin{1})
|
| | 85 | error(message('MATLAB:UNIQUE:InputClass'));
|
< 0.001 | 20 | 86 | end
|
< 0.001 | 20 | 87 | nrhs = nargin;
|
< 0.001 | 20 | 88 | if nrhs == 1
|
| | 89 | [varargout{1:nlhs}] = celluniqueR2012a(varargin{:});
|
< 0.001 | 20 | 90 | else
|
| | 91 | % acceptable combinations, with optional inputs denoted in []
|
| | 92 | % unique(A, ['rows'], ['first'/'last'], ['legacy'/'R2012a']),
|
| | 93 | % where the position of 'rows' and 'first'/'last' may be reversed
|
| | 94 | % unique(A, ['rows'], ['sorted'/'stable']),
|
| | 95 | % where the position of 'rows' and 'sorted'/'stable' may be reversed
|
< 0.001 | 20 | 96 | nflagvals = 7;
|
< 0.001 | 20 | 97 | flagvals = ["rows" "first" "last" "sorted" "stable" "legacy" "R2012a"];
|
| | 98 | % When a flag is found, note the index into varargin where it was found
|
< 0.001 | 20 | 99 | flaginds = zeros(1,nflagvals);
|
< 0.001 | 20 | 100 | for i = 2:nrhs
|
< 0.001 | 20 | 101 | flag = varargin{i};
|
0.003 | 20 | 102 | foundflag = matlab.internal.math.partialMatchString(flag,flagvals);
|
< 0.001 | 20 | 103 | if ~any(foundflag)
|
| | 104 | if ischar(flag)
|
| | 105 | error(message('MATLAB:UNIQUE:UnknownFlag',flag));
|
| | 106 | else
|
| | 107 | error(message('MATLAB:UNIQUE:UnknownInput'));
|
| | 108 | end
|
< 0.001 | 20 | 109 | end
|
| | 110 | % Only 1 occurrence of each allowed flag value
|
< 0.001 | 20 | 111 | if flaginds(foundflag)
|
| | 112 | error(message('MATLAB:UNIQUE:RepeatedFlag',flag));
|
< 0.001 | 20 | 113 | end
|
< 0.001 | 20 | 114 | flaginds(foundflag) = i;
|
< 0.001 | 20 | 115 | end
|
| | 116 |
|
| | 117 | % Only 1 of each of the paired flags
|
< 0.001 | 20 | 118 | if flaginds(2) && flaginds(3)
|
| | 119 | error(message('MATLAB:UNIQUE:OccurrenceConflict'))
|
< 0.001 | 20 | 120 | end
|
< 0.001 | 20 | 121 | if flaginds(4) && flaginds(5)
|
| | 122 | error(message('MATLAB:UNIQUE:SetOrderConflict'))
|
< 0.001 | 20 | 123 | end
|
< 0.001 | 20 | 124 | if flaginds(6) && flaginds(7)
|
| | 125 | error(message('MATLAB:UNIQUE:BehaviorConflict'))
|
< 0.001 | 20 | 126 | end
|
| | 127 | % 'legacy' and 'R2012a' flags must be trailing
|
< 0.001 | 20 | 128 | if flaginds(6) && flaginds(6)~=nrhs
|
| | 129 | error(message('MATLAB:UNIQUE:LegacyTrailing'))
|
< 0.001 | 20 | 130 | end
|
< 0.001 | 20 | 131 | if flaginds(7) && flaginds(7)~=nrhs
|
| | 132 | error(message('MATLAB:UNIQUE:R2012aTrailing'))
|
< 0.001 | 20 | 133 | end
|
| | 134 |
|
< 0.001 | 20 | 135 | if flaginds(4) || flaginds(5) % 'stable'/'sorted' specified
|
< 0.001 | 10 | 136 | if flaginds(2) || flaginds(3) % does not combine with 'first'/'last'
|
| | 137 | error(message('MATLAB:UNIQUE:SetOrderOccurrence'))
|
< 0.001 | 10 | 138 | end
|
< 0.001 | 10 | 139 | if flaginds(6) || flaginds(7) % does not combine with 'legacy'/'R2012a'
|
| | 140 | error(message('MATLAB:UNIQUE:SetOrderBehavior'))
|
< 0.001 | 10 | 141 | end
|
0.006 | 10 | 142 | [varargout{1:nlhs}] = celluniqueR2012a(varargin{1},logical(flaginds(1:5)));
|
< 0.001 | 10 | 143 | elseif flaginds(7) % trailing 'R2012a' specified
|
0.002 | 10 | 144 | [varargout{1:nlhs}] = celluniqueR2012a(varargin{1},logical(flaginds(1:5)));
|
| | 145 | elseif flaginds(6) % trailing 'legacy' specified
|
| | 146 | [varargout{1:nlhs}] = celluniquelegacy(varargin{1},logical(flaginds(1:3)));
|
| | 147 | else % 'R2012a' (default behavior, to be changed to 'R2012a' in future)
|
| | 148 | [varargout{1:nlhs}] = celluniqueR2012a(varargin{1},logical(flaginds(1:5)));
|
< 0.001 | 20 | 149 | end
|
< 0.001 | 20 | 150 | end
|
< 0.001 | 20 | 151 | end
|
Other subfunctions in this file are not included in this listing.