sortSorting numbers in an array
B = sort(A)[B, I] = sort(A)A in ascending order.A is a vector, it sorts along the longer dimension.A is a matrix or multi-dimensional array, it sorts along the first non-singleton dimension.A is complex, it sorts by moduli, and then by the phase angles if two elements have the same moduli.A is a character array, it sorts by the 16-bit scalar values.I contains indices of the sorted elements.B = sort(A, k)B = sort(A, direction)B = sort(A, k, direction)[B, I] = sort(A, k)[B, I] = sort(A, direction)[B, I] = sort(A, k, direction)A, B and I are as specified in the above usages.A along the k-th dimension in the order specified by direction.k should be a real positive integer scalar.k is not provided, it sorts along the first non-singleton dimension of A.direction should be either 'ascend' or 'descend'.direction is not provided, it sorts in ascending order.B = sort(A, name1, value1, ..., nameN, valueN)B = sort(A, k, name1, value1, ..., nameN, valueN)B = sort(A, direction, name1, value1, ..., nameN, valueN)B = sort(A, k, direction, name1, value1, ..., nameN, valueN)[B, I] = sort(A, name1, value1, ..., nameN, valueN)[B, I] = sort(A, k, name1, value1, ..., nameN, valueN)[B, I] = sort(A, direction, name1, value1, ..., nameN, valueN)[B, I] = sort(A, k, direction, name1, value1, ..., nameN, valueN)The inputs A, k and direction, and outputs B and I are as specified in the above usages.
The names should be either MissingPlacement or ComparisonMethod.
MissingPlacement should be either auto, first or last. See the table below for details.ComparisonMethod should be either auto, real or abs. See the table below for details.| Name | Value | Meaning |
|---|---|---|
MissingPlacement | auto | Default value. Missing elements come first when it sorts in the descending order. Missing elements come last when it sorts in the ascending order. |
| - | first | Missing elements come first. |
| - | last | Missing elements come last. |
ComparisonMethod | auto | Default value. Sort by real parts when A is real.Sort by moduli when A is complex. |
| - | real | Sort by real parts for real and complex numbers. Sort by imaginary parts when two complex numbers have the same real parts. |
| - | abs | Sort by absolute values for real numbers. Sort by moduli for complex numbers. If two complex numbers have equal moduli, it sorts by the phase angles. |
% Random ASCII values of captial letters
a = randi(26,1,20)+64;
% Convert to character array
c = char(a)
% Sorting
sort(c)
c =
BWIRYRNTDIPEDLJWPHPB
ans =
BBDDEHIIJLNPPPRRTWWY
A = rand(1,5)
[B,I] = sort(A)
A = 1e-1 ×
5.4456 1.3455 4.6364 2.0770 7.7713
B = 1e-1 ×
1.3455 2.0770 4.6364 5.4456 7.7713
I =
2.0000 4.0000 3.0000 1.0000 5.0000
A = rand(3,4)
[B,I] = sort(A)
A = 1e-1 ×
3.8998 3.8172 9.0582 3.1150
9.2799 1.9726 9.3383 0.7341
8.4704 0.5577 3.0939 5.0950
B = 1e-1 ×
3.8998 0.5577 3.0939 0.7341
8.4704 1.9726 9.0582 3.1150
9.2799 3.8172 9.3383 5.0950
I =
1.0000 3.0000 3.0000 2.0000
3.0000 2.0000 1.0000 1.0000
2.0000 1.0000 2.0000 3.0000
A = rand(3,4)
[B,I] = sort(A,2,'descend')
A = 1e-1 ×
0.5282 0.1593 2.9196 5.8984
4.7364 0.9964 0.1307 2.9491
9.0733 8.7286 6.5544 9.4573
B = 1e-1 ×
5.8984 2.9196 0.5282 0.1593
4.7364 2.9491 0.9964 0.1307
9.4573 9.0733 8.7286 6.5544
I =
4.0000 3.0000 1.0000 2.0000
1.0000 4.0000 2.0000 3.0000
4.0000 1.0000 2.0000 3.0000
[55357 56881]. The order of the scalar values remains unchanged when it is sorted in ascending order. Hence, it is encoded correctly in the sorted vector. Nevertheless, when it is sorted in descending order, it becomes [56881 55357]. Therefore, it cannot be encoded correctly (displaed as ��) in the sorted vector. ⚠️⚠️ As a rule of thumb, sorting an array with Unicode characters may give unexpected errors.sort('😱 oh my god!')
sort('😱 oh my god!','descend')
ans =
!dghmooy😱
ans =
��yoomhgd!