time | Calls | line |
---|
| | 228 | function [c,indA,indC] = celluniqueR2012a(a,options)
|
| | 229 | % 'R2012a' flag implementation
|
| | 230 |
|
| | 231 | % flagvals = {'rows' 'first' 'last' 'sorted' 'stable'};
|
< 0.001 | 20 | 232 | if nargin == 1
|
| | 233 | order = 'sorted';
|
< 0.001 | 20 | 234 | else
|
< 0.001 | 20 | 235 | if options(1) > 0
|
| | 236 | warning(message('MATLAB:UNIQUE:RowsFlagIgnored')); % 'rows' flag ignored for cellstrs.
|
< 0.001 | 20 | 237 | end
|
< 0.001 | 20 | 238 | if options(5) > 0
|
| | 239 | order = 'stable';
|
< 0.001 | 20 | 240 | elseif options(3) > 0
|
| | 241 | order = 'last';
|
< 0.001 | 20 | 242 | else % if options(4) > 0 || options(2) || sum(options(2:5) == 0)
|
< 0.001 | 20 | 243 | order = 'sorted'; %'first' and 'sorted' do the same thing
|
< 0.001 | 20 | 244 | end
|
< 0.001 | 20 | 245 | end
|
| | 246 |
|
| | 247 | % check if input is an array with each element a single row text array.
|
< 0.001 | 20 | 248 | if any(cellfun('size',a(:),1)>1)
|
| | 249 | error(message('MATLAB:UNIQUE:NotARowVector'));
|
< 0.001 | 20 | 250 | end
|
| | 251 |
|
| | 252 | % Determine if A is a row vector and the number of elements of A.
|
< 0.001 | 20 | 253 | rowvec = isrow(a);
|
< 0.001 | 20 | 254 | numelA = numel(a);
|
| | 255 |
|
< 0.001 | 20 | 256 | a = a(:);
|
| | 257 |
|
| | 258 | % Sort A and get indices.
|
< 0.001 | 20 | 259 | [sortA,indSortA] = sort(a);
|
| | 260 |
|
| | 261 |
|
| | 262 | % groupsSortA indicates the location of non-matching entries.
|
< 0.001 | 20 | 263 | groupsSortA = ~strcmp(sortA(1:end-1),sortA(2:end));
|
< 0.001 | 20 | 264 | groupsSortA = groupsSortA(:);
|
| | 265 |
|
< 0.001 | 20 | 266 | if ~isempty(a)
|
< 0.001 | 20 | 267 | if strcmp(order, 'last')
|
| | 268 | groupsSortA = [groupsSortA; true]; % Final element is always a member of unique list.
|
< 0.001 | 20 | 269 | else % strcmp(order, 'sorted') || strcmp(order, 'stable')
|
< 0.001 | 20 | 270 | groupsSortA = [true;groupsSortA]; % First element is always a member of unique list.
|
< 0.001 | 20 | 271 | end
|
< 0.001 | 20 | 272 | end
|
| | 273 |
|
| | 274 | % Extract unique elements
|
< 0.001 | 20 | 275 | if strcmp(order, 'stable')
|
| | 276 | invIndSortA = indSortA;
|
| | 277 | invIndSortA(invIndSortA) = 1:numelA; % Find inverse permutation.
|
| | 278 | logIndA = groupsSortA(invIndSortA); % Create new logical by indexing into groupsSortA.
|
| | 279 | c = a(logIndA); % Create unique list by indexing into unsorted a.
|
< 0.001 | 20 | 280 | else
|
< 0.001 | 20 | 281 | c = sortA(groupsSortA); % Create unique list by indexing into sorted list.
|
< 0.001 | 20 | 282 | end
|
| | 283 |
|
| | 284 | % Find indA.
|
< 0.001 | 20 | 285 | if nargout > 1
|
| | 286 | if strcmp(order, 'stable')
|
| | 287 | indA = find(logIndA); % Find the indices of the unsorted logical.
|
| | 288 | else
|
| | 289 | indA = indSortA(groupsSortA); % Find the indices of the sorted logical.
|
| | 290 | end
|
< 0.001 | 20 | 291 | end
|
| | 292 |
|
| | 293 | % Find indC.
|
< 0.001 | 20 | 294 | if nargout == 3
|
| | 295 | if isempty(a)
|
| | 296 | indC = zeros(0,1);
|
| | 297 | else
|
| | 298 | switch order
|
| | 299 | case 'last'
|
| | 300 | indC = cumsum([1;groupsSortA(1:end-1)]); % Lists position, starting at 1.
|
| | 301 | indC(indSortA) = indC; % Re-reference indC to indexing of sortA.
|
| | 302 | case 'sorted'
|
| | 303 | indC = cumsum(groupsSortA); % Lists position, starting at 1.
|
| | 304 | indC(indSortA) = indC; % Re-reference indC to indexing of sortA.
|
| | 305 | otherwise % 'stable'
|
| | 306 | [~,indSortC] = sort(c); % Sort C to get index.
|
| | 307 |
|
| | 308 | lengthGroupsSortA = diff(find([groupsSortA; true])); % Determine how many of each of the above indices there are in IC.
|
| | 309 |
|
| | 310 | diffIndSortC = diff(indSortC); % Get the correct amount of each index.
|
| | 311 | diffIndSortC = [indSortC(1); diffIndSortC];
|
| | 312 |
|
| | 313 | indLengthGroupsSortA = cumsum([1; lengthGroupsSortA]);
|
| | 314 | indLengthGroupsSortA(end) = [];
|
| | 315 |
|
| | 316 | indCOrderedBySortA(indLengthGroupsSortA,1) = diffIndSortC; % Since indCOrderedBySortA is not already established as a column,
|
| | 317 | if sum(lengthGroupsSortA) ~= length(indCOrderedBySortA);
|
| | 318 | indCOrderedBySortA(sum(lengthGroupsSortA),1) = 0;
|
| | 319 | end
|
| | 320 |
|
| | 321 | indCOrderedBySortA = cumsum(indCOrderedBySortA);
|
| | 322 | indC = indCOrderedBySortA(invIndSortA); % Reorder the list of indices to the unsorted order.
|
| | 323 | end
|
| | 324 | end
|
< 0.001 | 20 | 325 | end
|
| | 326 |
|
< 0.001 | 20 | 327 | if rowvec
|
| | 328 | c = c.';
|
< 0.001 | 20 | 329 | end
|
< 0.001 | 20 | 330 | end
|
Other subfunctions in this file are not included in this listing.