time | Calls | line |
---|
| | 1 | function [c, ia, ib] = setxor(A, B, flag1, flag2)
|
| | 2 | %SETXOR Set exclusive-or.
|
| | 3 | % C = SETXOR(A,B) for vectors A and B, returns the values that are not
|
| | 4 | % in the intersection of A and B with no repetitions. C will be sorted.
|
| | 5 | %
|
| | 6 | % C = SETXOR(A,B,'rows') for matrices A and B with the same number of
|
| | 7 | % columns, returns the rows that are not in the intersection of A and B.
|
| | 8 | % The rows of the matrix C will be in sorted order.
|
| | 9 | %
|
| | 10 | % [C,IA,IB] = SETXOR(A,B) also returns index vectors IA and IB such that
|
| | 11 | % C is a sorted combination of the values A(IA) and B(IB). If there
|
| | 12 | % are repeated values that are not in the intersection in A or B then the
|
| | 13 | % index of the first occurrence of each repeated value is returned.
|
| | 14 | %
|
| | 15 | % [C,IA,IB] = SETXOR(A,B,'rows') also returns index vectors IA and IB
|
| | 16 | % such that C is the sorted combination of rows A(IA,:) and B(IB,:).
|
| | 17 | %
|
| | 18 | % [C,IA,IB] = SETXOR(A,B,'stable') for arrays A and B, returns the values
|
| | 19 | % of C in the same order that they appear in A, then B.
|
| | 20 | % [C,IA,IB] = SETXOR(A,B,'sorted') returns the values of C in sorted
|
| | 21 | % order.
|
| | 22 | % If A and B are row vectors, then C will be a row vector as well,
|
| | 23 | % otherwise C will be a column vector. IA and IB are column vectors.
|
| | 24 | % If there are repeated values that are not in the intersection of A and
|
| | 25 | % B, then the index of the first occurrence of each repeated value is
|
| | 26 | % returned.
|
| | 27 | %
|
| | 28 | % [C,IA,IB] = SETXOR(A,B,'rows','stable') returns the rows of C in the
|
| | 29 | % same order that they appear in A, then B.
|
| | 30 | % [C,IA,IB] = SETXOR(A,B,'rows','sorted') returns the rows of C in sorted
|
| | 31 | % order.
|
| | 32 | %
|
| | 33 | % The behavior of SETXOR has changed. This includes:
|
| | 34 | % - occurrence of indices in IA and IB switched from last to first
|
| | 35 | % - orientation of vector C
|
| | 36 | % - IA and IB will always be column index vectors
|
| | 37 | % - tighter restrictions on combinations of classes
|
| | 38 | %
|
| | 39 | % If this change in behavior has adversely affected your code, you may
|
| | 40 | % preserve the previous behavior with:
|
| | 41 | %
|
| | 42 | % [C,IA,IB] = SETXOR(A,B,'legacy')
|
| | 43 | % [C,IA,IB] = SETXOR(A,B,'rows','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 | % b = [1 1 1 3 3 3 3 3 4 4 4 4 4 10 10 10]
|
| | 49 | % [c1,ia1,ib1] = setxor(a,b)
|
| | 50 | % % returns
|
| | 51 | % c1 = [2 3 5 6 7 8 9 10], ia1 = [20 17 14 11 7 1]', ib1 = [4 14]'
|
| | 52 | %
|
| | 53 | % [c2,ia2,ib2] = setxor(a,b,'stable')
|
| | 54 | % % returns
|
| | 55 | % c2 = [9 8 7 6 5 2 3 10], ia2 = [1 7 11 14 17 20]', ib2 = [4 14]'
|
| | 56 | %
|
| | 57 | % c = setxor([1 NaN 2 3],[3 4 NaN 1])
|
| | 58 | % % NaNs compare as not equal, so this returns
|
| | 59 | % c = [2 4 NaN NaN]
|
| | 60 | %
|
| | 61 | % Class support for inputs A and B, where A and B must be of the same
|
| | 62 | % class unless stated otherwise:
|
| | 63 | % - logical, char, all numeric classes (may combine with double arrays)
|
| | 64 | % - cell arrays of strings (may combine with char arrays)
|
| | 65 | % -- 'rows' option is not supported for cell arrays
|
| | 66 | % - objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE
|
| | 67 | % -- including heterogeneous arrays derived from the same root class
|
| | 68 | %
|
| | 69 | % See also UNIQUE, UNION, INTERSECT, SETDIFF, ISMEMBER, SORT, SORTROWS.
|
| | 70 |
|
| | 71 | % Copyright 1984-2019 The MathWorks, Inc.
|
| | 72 |
|
| | 73 | % Convert string flags to char flags to dispatch to the right method
|
< 0.001 | 2 | 74 | if nargin == 3 && isstring(flag1)
|
| | 75 | flag1 = convertFlag(flag1);
|
| | 76 | if nargout <= 1
|
| | 77 | c = setxor(A, B, flag1);
|
| | 78 | else
|
| | 79 | [c, ia, ib] = setxor(A, B, flag1);
|
| | 80 | end
|
| | 81 | return
|
< 0.001 | 2 | 82 | end
|
| | 83 |
|
< 0.001 | 2 | 84 | if nargin == 4 && (isstring(flag1) || isstring(flag2))
|
| | 85 | if isstring(flag1)
|
| | 86 | flag1 = convertFlag(flag1);
|
| | 87 | end
|
| | 88 | if isstring(flag2)
|
| | 89 | flag2 = convertFlag(flag2);
|
| | 90 | end
|
| | 91 | if nargout <= 1
|
| | 92 | c = setxor(A, B, flag1, flag2);
|
| | 93 | else
|
| | 94 | [c, ia, ib] = setxor(A, B, flag1, flag2);
|
| | 95 | end
|
| | 96 | return
|
< 0.001 | 2 | 97 | end
|
< 0.001 | 2 | 98 | if isstring(A) || isstring(B)
|
| | 99 | if ~ischar(A) && ~iscellstr(A) && ~isstring(A)
|
| | 100 | firstInput = getString(message('MATLAB:string:FirstInput'));
|
| | 101 | error(message('MATLAB:string:MustBeCharCellArrayOrString', firstInput));
|
| | 102 | elseif ~ischar(B) && ~iscellstr(B) && ~isstring(B)
|
| | 103 | secondInput = getString(message('MATLAB:string:SecondInput'));
|
| | 104 | error(message('MATLAB:string:MustBeCharCellArrayOrString', secondInput));
|
| | 105 | end
|
| | 106 | A = string(A);
|
| | 107 | B = string(B);
|
< 0.001 | 2 | 108 | end
|
| | 109 |
|
< 0.001 | 2 | 110 | if nargin == 2
|
< 0.001 | 2 | 111 | if nargout <= 1
|
0.010 | 2 | 112 | c = setxorR2012a(A, B);
|
| | 113 | else
|
| | 114 | [c, ia, ib] = setxorR2012a(A, B);
|
< 0.001 | 2 | 115 | end
|
| | 116 | else
|
| | 117 | % acceptable combinations, with optional inputs denoted in []
|
| | 118 | % setxor(A,B, ['rows'], ['legacy'/'R2012a']),
|
| | 119 | % setxor(A,B, ['rows'], ['sorted'/'stable']),
|
| | 120 | % where the position of 'rows' and 'sorted'/'stable' may be reversed
|
| | 121 | nflagvals = 5;
|
| | 122 | % When a flag is found, note the index into varargin where it was found
|
| | 123 | flaginds = parseFlags(zeros(1,nflagvals), flag1, 3);
|
| | 124 | if nargin > 3
|
| | 125 | flaginds = parseFlags(flaginds, flag2, 4);
|
| | 126 | end
|
| | 127 |
|
| | 128 | % Only 1 of each of the paired flags
|
| | 129 | if flaginds(2) && flaginds(3)
|
| | 130 | error(message('MATLAB:SETXOR:SetOrderConflict'))
|
| | 131 | end
|
| | 132 | if flaginds(4) && flaginds(5)
|
| | 133 | error(message('MATLAB:SETXOR:BehaviorConflict'))
|
| | 134 | end
|
| | 135 | % 'legacy' and 'R2012a' flags must be trailing
|
| | 136 | if flaginds(4) && flaginds(4)~=nargin
|
| | 137 | error(message('MATLAB:SETXOR:LegacyTrailing'))
|
| | 138 | end
|
| | 139 | if flaginds(5) && flaginds(5)~=nargin
|
| | 140 | error(message('MATLAB:SETXOR:R2012aTrailing'))
|
| | 141 | end
|
| | 142 |
|
| | 143 | if flaginds(2) || flaginds(3) % 'stable'/'sorted' specified
|
| | 144 | if flaginds(4) || flaginds(5) % does not combine with 'legacy'/'R2012a'
|
| | 145 | error(message('MATLAB:SETXOR:SetOrderBehavior'))
|
| | 146 | end
|
| | 147 | if nargout <= 1
|
| | 148 | c = setxorR2012a(A, B, logical(flaginds(1:3)));
|
| | 149 | else
|
| | 150 | [c, ia, ib] = setxorR2012a(A, B, logical(flaginds(1:3)));
|
| | 151 | end
|
| | 152 | elseif flaginds(5) % trailing 'R2012a' specified
|
| | 153 | if nargout <= 1
|
| | 154 | c = setxorR2012a(A, B, logical(flaginds(1:3)));
|
| | 155 | else
|
| | 156 | [c, ia, ib] = setxorR2012a(A, B, logical(flaginds(1:3)));
|
| | 157 | end
|
| | 158 | elseif flaginds(4) % trailing 'legacy' specified
|
| | 159 | if nargout <= 1
|
| | 160 | c = setxorlegacy(A, B, logical(flaginds(1)));
|
| | 161 | else
|
| | 162 | [c, ia, ib] = setxorlegacy(A, B, logical(flaginds(1)));
|
| | 163 | end
|
| | 164 | else % 'R2012a' (default behavior)
|
| | 165 | if nargout <= 1
|
| | 166 | c = setxorR2012a(A, B, logical(flaginds(1:3)));
|
| | 167 | else
|
| | 168 | [c, ia, ib] = setxorR2012a(A, B, logical(flaginds(1:3)));
|
| | 169 | end
|
| | 170 | end
|
< 0.001 | 2 | 171 | end
|
< 0.001 | 2 | 172 | end
|
Other subfunctions in this file are not included in this listing.