[MATLAB Issue 73] # Source code sharing | Collection of different types of data arrangement methods based on MATLAB
Study reference link
1. Sort rows of array (cell/string/category/table)
2. Sort text by character code
3. Sort file name, folder name or file path
4. Sort string/cell / Sort the elements of a categorical array
1. Improved arrangement
X = {
'A2','X';'A10','Y';'A10','X';'A1','X'};
sortrows(X) % 错误排序
{
'A1' } {
'X'}
{
'A10'} {
'X'}
{
'A10'} {
'Y'}
{
'A2' } {
'X'}
natsortrows(X) % 正确排序
{
'A1' } {
'X'}
{
'A2' } {
'X'}
{
'A10'} {
'X'}
{
'A10'} {
'Y'}
1. The array to be sorted
The first input must be one of the following array types:
*字符矩阵,
*字符行矢量的单元阵列,
*一个单元格数组,其列仅由字符行组成
矢量或纯数字标量:
The first input must be a matrix (i.e. 2D only).
The sorted array will be returned as the first output parameter, for example:
A = {
'B','2','X';'A','100','X';'B','10','X';'A','2','Y';'A','20','X'};
natsortrows(categorical(A)) %
A 2 Y
A 20 X
A 100 X
B 2 X
B 10 X
2. Decimal expressions
B = {
'1.3','X';'1.10','X';'1.2','X'}
natsortrows(B)%默认情况下仅匹配整数。
{
'1.2' } {
'X'}
{
'1.3' } {
'X'}
{
'1.10'} {
'X'}
natsortrows(B, '\d+\.?\d*')%匹配小数。
{
'1.10'} {
'X'}
{
'1.2' } {
'X'}
{
'1.3' } {
'X'}
3. Specify the column to be sorted
If desired, you can specify the columns to sort on using one of the following methods:
A logical vector of column indices of the input array.
A numeric vector of indices into the columns of the input array, where positive integers sort the corresponding column in ascending order and
negative integers sort the corresponding column in descending order. This corresponds to the |SORTROWS|columns| option.
For example, the second column is sorted in ascending order and the third column is sorted in descending order:
sortrows(A, [2,-3]) %编号顺序错误。
{
'B'} {
'10' } {
'X'}
{
'A'} {
'100'} {
'X'}
{
'A'} {
'2' } {
'Y'}
{
'B'} {
'2' } {
'X'}
{
'A'} {
'20' } {
'X'}
natsortrows(A, [], [2,-3])%正确的数字顺序。
{
'A'} {
'2' } {
'Y'}
{
'B'} {
'2' } {
'X'}
{
'B'} {
'10' } {
'X'}
{
'A'} {
'20' } {
'X'}
{
'A'} {
'100'} {
'X'}
4. Specify the sorting direction
The |SORTROWS||direction| option is supported, where the character vectors |'ascend'|, |'descend'| and/or |'ignore'| specify the sort direction of the column to be sorted (this means that the column or by
| column| or |vars| column specified by option).
In these examples, the second column is sorted in ascending order, and the third column is sorted in descending order:
natsortrows(A, [], [2,3], {
'ascend','descend'})
{
'A'} {
'2' } {
'Y'}
{
'B'} {
'2' } {
'X'}
{
'B'} {
'10' } {
'X'}
{
'A'} {
'20' } {
'X'}
{
'A'} {
'100'} {
'X'}
natsortrows(A, [], [false,true,true], {
'ascend','descend'})
{
'A'} {
'2' } {
'Y'}
{
'B'} {
'2' } {
'X'}
{
'B'} {
'10' } {
'X'}
{
'A'} {
'20' } {
'X'}
{
'A'} {
'100'} {
'X'}
natsortrows(A, [], {
'ignore','ascend','descend'})
{
'A'} {
'2' } {
'Y'}
{
'B'} {
'2' } {
'X'}
{
'B'} {
'10' } {
'X'}
{
'A'} {
'20' } {
'X'}
{
'A'} {
'100'} {
'X'}
5.Table name
The |SORTROWS|| table supports the 'RowNames'| option, either as literal text |'RowNames'| or as the name of the first dimension of the table (i.e. the
|SORTROWS||rowDimName| option).
T = cell2table(A,'RowNames',{
'R20','R1','R10','R2','R9'},'VariableNames',{
'V1','V2','V3'})
natsortrows(T, [], 'RowNames')
V1 V2 V3
_____ _______ _____
R1 {
'A'} {
'100'} {
'X'}
R2 {
'A'} {
'2' } {
'Y'}
R9 {
'A'} {
'20' } {
'X'}
R10 {
'B'} {
'10' } {
'X'}
R20 {
'B'} {
'2' } {
'X'}
natsortrows(T, [], 'Row') %第一维度名称
V1 V2 V3
_____ _______ _____
R1 {
'A'} {
'100'} {
'X'}
R2 {
'A'} {
'2' } {
'Y'}
R9 {
'A'} {
'20' } {
'X'}
R10 {
'B'} {
'10' } {
'X'}
R20 {
'B'} {
'2' } {
'X'}
6.Table variable name
The |SORTROWS||vars| option supports
logical or numeric vectors for tables, that is, indexes in table variables.
The name of a table variable to sort.
A cell array of one or more names of table variables to sort.
natsortrows(T, [], {
'V2','V3'},{
'ascend','descend'})
V1 V2 V3
_____ _______ _____
R2 {
'A'} {
'2' } {
'Y'}
R20 {
'B'} {
'2' } {
'X'}
R10 {
'B'} {
'10' } {
'X'}
R9 {
'A'} {
'20' } {
'X'}
R1 {
'A'} {
'100'} {
'X'}
natsortrows(T, [], [2,3], {
'ascend','descend'})
V1 V2 V3
_____ _______ _____
R2 {
'A'} {
'2' } {
'Y'}
R20 {
'B'} {
'2' } {
'X'}
R10 {
'B'} {
'10' } {
'X'}
R9 {
'A'} {
'20' } {
'X'}
R1 {
'A'} {
'100'} {
'X'}
7. Numeric scalars in cell arrays
When sorting a cell array, any column consisting only of numbers or logical scalars can be sorted by selecting the |'SortNum'| option.
Example of a mixed cell array where column 1 consists entirely of character row vectors and column 2 consists entirely of numeric scalars:
C = {
'A2',2;'A10',1;'A2',1}
natsortrows(C,[],'SortNum')
{
'A2' } {
[1.00]}
{
'A2' } {
[2.00]}
{
'A10'} {
[1.00]}
8. Optional parameters
Further input is passed directly to |NATSORT|, thereby controlling case sensitivity, sort direction, and other options. See |NATSORT| Help for explanations and examples of support options:
D = {
'B','X';'10','X';'1','X';'A','X';'2','X'}
natsortrows(D, [], 'descend')
{
'B' } {
'X'}
{
'A' } {
'X'}
{
'10'} {
'X'}
{
'2' } {
'X'}
{
'1' } {
'X'}
natsortrows(D, [], 'char<num')
{
'A' } {
'X'}
{
'B' } {
'X'}
{
'1' } {
'X'}
{
'2' } {
'X'}
{
'10'} {
'X'}
9. Sort index
The second output argument is a numeric array with sorted index |ndx| such that |Y=X(ndx,:)| where |Y=natsortrows(X)|:
E = {
'abc2xyz','Y';'abc10xy99','X';'abc2xyz','X';'abc1xyz','X'}
[out,ndx] = natsortrows(E)
out =
{
'abc1xyz' } {
'X'}
{
'abc2xyz' } {
'X'}
{
'abc2xyz' } {
'Y'}
{
'abc10xy99'} {
'X'}
ndx =
4.00
3.00
1.00
2.00
10. Debug Array
The third output is a cell vector of a cell array corresponding to a column of the input cell array |X|.
The cell array contains all matching numbers (converted to numbers using the specified |SSCANF| format) and all non-numeric substrings. These cell arrays help confirm that decimal expressions are recognizing numbers correctly.
[~,~,dbg] = natsortrows(E);
{
'abc'} {
[ 2.00]} {
'xyz'} {
0×0 double}
{
'abc'} {
[10.00]} {
'xy' } {
[ 99.00]}
{
'abc'} {
[ 2.00]} {
'xyz'} {
0×0 double}
{
'abc'} {
[ 1.00]} {
'xyz'} {
0×0 double}
dbg{
:}
{
'Y'}
{
'X'}
{
'X'}
{
'X'}
11. Decimal expression: decimal decimal, E notation, +/- symbols.
|NATSORTROWS|Number matching can be customized to detect digit decimal fractions, E notation, +/- signs, binary/hex, or other desired functionality. Use appropriate decimal expressions, see |NATSORT| for details and examples.
F = {
'v10.2','b'; 'v2.5','b'; 'v2.40','a'; 'v1.9','b'}
natsortrows(F) %默认匹配整数,例如版本号。
{
'v1.9' } {
'b'}
{
'v2.5' } {
'b'}
{
'v2.40'} {
'a'}
{
'v10.2'} {
'b'}
natsortrows(F,'\d+\.?\d*') %与小数部分匹配
{
'v1.9' } {
'b'}
{
'v2.40'} {
'a'}
{
'v2.5' } {
'b'}
{
'v10.2'} {
'b'}
12.Interactive decimal expression tool
Decimal expressions are powerful and compact, but using them correctly isn't always easy. One help is to download the interactive tool https://www.mathworks.com/matlabcentral/fileexchange/48930|IREGEXP|
which allows you to quickly try different decimal expressions and see all the output displayed and updated as you type.
https://www.mathworks.com/help/matlab/ref/regexp.html|REGEXP|
2. Basic arrangement
1. Sort matrix rows
Create a matrix and sort the matrix rows in ascending order based on the elements in the first column. When the first column contains duplicate elements, sortrows sorts based on the elements in the second column. For duplicate elements in the second column, sortrows sorts based on the third column, and so on.
rng default;
A = floor(rand([6 7])*100);
A(1:4,1) = 95; A(5:6,1) = 76; A(2:4,2) = 7; A(3,3) = 48
A = 6×7
95 27 95 79 67 70 69
95 7 48 95 75 3 31
95 7 48 65 74 27 95
95 7 14 3 39 4 3
76 15 42 84 65 9 43
76 97 91 93 17 82 38
B = sortrows(A)
B = 6×7
76 15 42 84 65 9 43
76 97 91 93 17 82 38
95 7 14 3 39 4 3
95 7 48 65 74 27 95
95 7 48 95 75 3 31
95 27 95 79 67 70 69
Sorts the rows of A based on the value in the second column. When a specified column contains duplicate elements, the corresponding rows retain their original order.
C = sortrows(A,2)
C = 6×7
95 7 48 95 75 3 31
95 7 48 65 74 27 95
95 7 14 3 39 4 3
76 15 42 84 65 9 43
95 27 95 79 67 70 69
76 97 91 93 17 82 38
Sorts the rows of A based on the elements in the first column, or by the seventh column if the first column contains the same element.
D = sortrows(A,[1 7])
D = 6×7
76 97 91 93 17 82 38
76 15 42 84 65 9 43
95 7 14 3 39 4 3
95 7 48 95 75 3 31
95 27 95 79 67 70 69
95 7 48 65 74 27 95
Sort the rows of A in descending order based on the elements in the fourth column, and display the output vector index to see how the rows are rearranged.
[E,index] = sortrows(A,4,‘descend’)
E = 6×7
95 7 48 95 75 3 31
76 97 91 93 17 82 38
76 15 42 84 65 9 43
95 27 95 79 67 70 69
95 7 48 65 74 27 95
95 7 14 3 39 4 3
index = 6×1
2
6
5
1
3
4
2. Sort cell array rows
Create a 6-by-2 cell array of character vectors and sort its rows. The result is an alphabetical list sorted by both country and name.
A = {‘Germany’ ‘Lukas’; ‘USA’ ‘William’; ‘USA’ ‘Andrew’; …
‘Germany’ ‘Andreas’; ‘USA’ ‘Olivia’; ‘Germany’ ‘Julia’}
A = 6x2 cell
{‘Germany’} {‘Lukas’ }
{‘USA’ } {‘William’}
{‘USA’ } {‘Andrew’ }
{‘Germany’} {‘Andreas’}
{‘USA’ } {‘Olivia’ }
{‘Germany’} {‘Julia’ }
B = sortrows(A)
B = 6x2 cell
{‘Germany’} {‘Andreas’}
{‘Germany’} {‘Julia’ }
{‘Germany’} {‘Lukas’ }
{‘USA’ } {‘Andrew’ }
{‘USA’ } {‘Olivia’ }
{‘USA’ } {‘William’}
Sort by country first, then name, in descending order.
C = sortrows(A,[1 2],{‘ascend’ ‘descend’})
C = 6x2 cell
{‘Germany’} {‘Lukas’ }
{‘Germany’} {‘Julia’ }
{‘Germany’} {‘Andreas’}
{‘USA’ } {‘William’}
{‘USA’ } {‘Olivia’ }
{‘USA’ } {‘Andrew’ }
3. Sort table rows
Sort table rows by variable value.
Create a table that contains four variables that list information about five patients.
LastName = {‘Smith’;‘Johnson’;‘Williams’;‘Jones’;‘Brown’};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
tblA = table(Age,Height,Weight,BloodPressure,‘RowNames’,LastName)
tblA=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
Johnson 43 69 163 109 77
Williams 38 64 131 125 83
Jones 40 67 133 117 75
Brown 49 64 119 122 80
Sort the rows of this table. The sortrows function first sorts the rows in ascending order by the variable Age, and then sorts two rows with equal ages by the variable Height.
tblB = sortrows(tblA)
tblB=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Williams 38 64 131 125 83
Smith 38 71 176 124 93
Jones 40 67 133 117 75
Johnson 43 69 163 109 77
Brown 49 64 119 122 80
4. Sort table rows by row name
Create a table that contains four variables that list information about five patients.
LastName = {‘Smith’;‘Johnson’;‘Williams’;‘Jones’;‘Brown’};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
tblA = table(Age,Height,Weight,BloodPressure,‘RowNames’,LastName)
tblA=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
Johnson 43 69 163 109 77
Williams 38 64 131 125 83
Jones 40 67 133 117 75
Brown 49 64 119 122 80
Sorts table rows in ascending order based on row names and returns an index vector describing how the rows are rearranged.
[tblB,index] = sortrows(tblA,‘RowNames’)
tblB=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Brown 49 64 119 122 80
Johnson 43 69 163 109 77
Jones 40 67 133 117 75
Smith 38 71 176 124 93
Williams 38 64 131 125 83
index = 5×1
5
2
4
1
3
5. Sort table rows by variable
Create a table that contains four variables that list information about five patients.
LastName = {‘Sweet’;‘Jacobson’;‘Wang’;‘Joiner’;‘Berger’};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
tblA = table(Age,Height,Weight,BloodPressure,‘RowNames’,LastName)
tblA=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Sweet 38 71 176 124 93
Jacobson 43 69 163 109 77
Wang 38 64 131 125 83
Joiner 40 67 133 117 75
Berger 49 64 119 122 80
Sort the table rows in ascending order by Height and then in descending order by Weight.
tblB = sortrows(tblA,{‘Height’,‘Weight’},{‘ascend’,‘descend’})
tblB=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Wang 38 64 131 125 83
Berger 49 64 119 122 80
Joiner 40 67 133 117 75
Jacobson 43 69 163 109 77
Sweet 38 71 176 124 93
6. Table with missing elements
Create a table that contains four variables that list information about five patients. The Weight variable contains missing values.
LastName = {‘Sweet’;‘Jacobson’;‘Wang’;‘Joiner’;‘Berger’};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;NaN;131;133;NaN];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
tblA = table(Age,Height,Weight,BloodPressure,‘RowNames’,LastName)
tblA=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Sweet 38 71 176 124 93
Jacobson 43 69 NaN 109 77
Wang 38 64 131 125 83
Joiner 40 67 133 117 75
Berger 49 64 NaN 122 80
Sorts table rows in ascending order by Weight, placing rows containing NaN first.
tblB = sortrows(tblA,‘Weight’,‘MissingPlacement’,‘first’)
tblB=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Jacobson 43 69 NaN 109 77
Berger 49 64 NaN 122 80
Wang 38 64 131 125 83
Joiner 40 67 133 117 75
Sweet 38 71 176 124 93
7. Sort timetable rows
Create a timetable and sort the rows by row time.
TimeDuration = [hours(3) hours(2) hours(1) hours(5) hours(6)]';
TT = timetable(TimeDuration,[98;97.5;97.9;98.1;101],[120;111;119;117;118]);
B = sortrows(TT,‘TimeDuration’)
B=5×2 timetable
TimeDuration Var1 Var2
____________ ____ ____
1 hr 97.9 119
2 hr 97.5 111
3 hr 98 120
5 hr 98.1 117
6 hr 101 118
3. Code acquisition
Just reply "Issue 73" to the CSDN backend private message.