time | Calls | line |
---|
| | 1 | function [b,idx] = sortrows(a,varargin)
|
| | 2 | %SORTROWS Sort rows of a table.
|
| | 3 | % B = SORTROWS(A) returns a copy of the table A, with the rows sorted in
|
| | 4 | % ascending order by all of the variables in A. The rows in B are sorted
|
| | 5 | % first by the first variable, next by the second variable, and so on.
|
| | 6 | % Each variable in A must be a valid input to SORT, or, if the variable
|
| | 7 | % has multiple columns, to the MATLAB SORTROWS function or to its own
|
| | 8 | % SORTROWS method.
|
| | 9 | %
|
| | 10 | % B = SORTROWS(A,VARS) sorts the rows in A by the variables specified by
|
| | 11 | % VARS. VARS must be a positive integer, a vector of positive integers, a
|
| | 12 | % variable name, a cell array containing one or more variable names, or a
|
| | 13 | % logical vector. VARS can also include the name of the row dimension, i.e.
|
| | 14 | % A.Properties.DimensionNames{1}, to sort by row names as well as by data
|
| | 15 | % variables. By default, the row dimension name is 'Row'.
|
| | 16 | %
|
| | 17 | % VARS can also contain a mix of positive and negative integers. If an
|
| | 18 | % element of VARS is positive, the corresponding variable in A will be
|
| | 19 | % sorted in ascending order; if an element of VARS is negative, the
|
| | 20 | % corresponding variable in A will be sorted in descending order. These
|
| | 21 | % signs are ignored if you provide the DIRECTION input described below.
|
| | 22 | %
|
| | 23 | % B = SORTROWS(A,'RowNames') sorts the rows in A by the row names.
|
| | 24 | %
|
| | 25 | % B = SORTROWS(A,VARS,DIRECTION) also specifies the sort direction(s):
|
| | 26 | % 'ascend' - (default) Sorts in ascending order.
|
| | 27 | % 'descend' - Sorts in descending order.
|
| | 28 | % SORTROWS sorts A in ascending or descending order according to all
|
| | 29 | % variables specified by VARS. You can also use a different direction for
|
| | 30 | % each variable by specifying multiple 'ascend' and 'descend' directions,
|
| | 31 | % for example, SORTROWS(X,[2 3],{'ascend' 'descend'}).
|
| | 32 | % Specify VARS as 1:SIZE(A,2) to sort using all variables.
|
| | 33 | %
|
| | 34 | % B = SORTROWS(A,VARS,...,'MissingPlacement',M) specifies where to place
|
| | 35 | % the missing elements (NaN/NaT/<undefined>/<missing>). M must be:
|
| | 36 | % 'auto' - (default) Places missing elements last for ascending sort
|
| | 37 | % and first for descending sort.
|
| | 38 | % 'first' - Places missing elements first.
|
| | 39 | % 'last' - Places missing elements last.
|
| | 40 | %
|
| | 41 | % B = SORTROWS(A,VARS,...,'ComparisonMethod',C) specifies how to sort
|
| | 42 | % complex numbers. The comparison method C must be:
|
| | 43 | % 'auto' - (default) Sorts real numbers according to 'real', and
|
| | 44 | % complex numbers according to 'abs'.
|
| | 45 | % 'real' - Sorts according to REAL(A). Elements with equal real parts
|
| | 46 | % are then sorted by IMAG(A).
|
| | 47 | % 'abs' - Sorts according to ABS(A). Elements with equal magnitudes
|
| | 48 | % are then sorted by ANGLE(A).
|
| | 49 | %
|
| | 50 | % [B,I] = SORTROWS(A,...) also returns an index vector I which describes
|
| | 51 | % the order of the sorted rows, namely, B = A(I,:).
|
| | 52 | %
|
| | 53 | % See also ISSORTEDROWS, UNIQUE.
|
| | 54 |
|
| | 55 | % Copyright 2012-2020 The MathWorks, Inc.
|
| | 56 |
|
0.021 | 3 | 57 | [vars,varData,sortMode,varargin] = sortrowsFlagChecks(false,a,varargin{:});
|
| | 58 |
|
| | 59 | % Sort on each index variable, last to first. Since sort is stable, the
|
| | 60 | % result is as if they were sorted all together.
|
< 0.001 | 3 | 61 | if isequal(vars,0) % fast special case for simple row labels cases
|
| | 62 | rowLabels = varData{1};
|
| | 63 |
|
| | 64 | % If sorting by RowNames with no labels fast exit
|
| | 65 | if (~a.rowDim.hasLabels)
|
| | 66 | b = a;
|
| | 67 | return
|
| | 68 | end
|
| | 69 |
|
| | 70 | if sortMode == 1
|
| | 71 | [~,idx] = sort(rowLabels,varargin{:});
|
| | 72 | else % sortMode == 2
|
| | 73 | if iscell(rowLabels)
|
| | 74 | [~,idx] = sortrows(rowLabels,-1,varargin{:}); % cellstr does not support 'descend'.
|
| | 75 | else
|
| | 76 | [~,idx] = sort(rowLabels,'descend',varargin{:});
|
| | 77 | end
|
| | 78 | end
|
| 3 | 79 | else
|
< 0.001 | 3 | 80 | sortModeStrs = {'ascend','descend'};
|
< 0.001 | 3 | 81 | idx = (1:a.rowDim.length)';
|
< 0.001 | 3 | 82 | for j = length(vars):-1:1
|
< 0.001 | 5 | 83 | var_j = varData{j};
|
< 0.001 | 5 | 84 | if ~ismatrix(var_j)
|
| | 85 | error(message('MATLAB:table:sortrows:NDVar',a.varDim.labels{vars(j)}));
|
< 0.001 | 5 | 86 | elseif matlab.internal.datatypes.istabular(var_j)
|
| | 87 | % Error gracefully when trying to sort tables of tables
|
| | 88 | error(message('MATLAB:table:sortrows:SortOnVarFailed',a.varDim.labels{vars(j)},class(var_j)));
|
< 0.001 | 5 | 89 | end
|
< 0.001 | 5 | 90 | var_j = var_j(idx,:);
|
| | 91 | % cell/sort is only for cellstr, use sortrows for cell always.
|
< 0.001 | 5 | 92 | if ~iscell(var_j) && isvector(var_j) && (size(var_j,2) == 1)
|
< 0.001 | 5 | 93 | try
|
< 0.001 | 5 | 94 | [~,ord] = sort(var_j,1,sortModeStrs{sortMode(j)},varargin{:});
|
| | 95 | catch ME
|
| | 96 | throw(addCause(...
|
| | 97 | MException(message('MATLAB:table:sortrows:SortOnVarFailed',a.varDim.labels{vars(j)},class(var_j))),...
|
| | 98 | ME));
|
< 0.001 | 5 | 99 | end
|
| | 100 | else % multi-column, or cell
|
| | 101 | % Sort by all columns, all either ascending or descending
|
| | 102 | cols = (1:size(var_j,2)) * 2*(1.5-sortMode(j));
|
| | 103 | try
|
| | 104 | [~,ord] = sortrows(var_j,cols,varargin{:});
|
| | 105 | catch ME
|
| | 106 | throw(addCause(...
|
| | 107 | MException(message('MATLAB:table:sortrows:SortrowsOnVarFailed',a.varDim.labels{vars(j)},class(var_j))),...
|
| | 108 | ME));
|
| | 109 | end
|
< 0.001 | 5 | 110 | end
|
< 0.001 | 5 | 111 | idx = idx(ord);
|
< 0.001 | 5 | 112 | end
|
| 3 | 113 | end
|
| | 114 |
|
0.017 | 3 | 115 | b = a.subsrefParens({idx ':'});
|
Other subfunctions in this file are not included in this listing.