SORTSET (v2.0)

版本 1.0.0.0 (2.8 KB) 作者: Jos (10584)
Sort a set according to a user-defined order
2.1K 次下载
更新时间 2008/10/20

查看许可证

SORTSET Sort a set according to a user-defined order

For vectors, R = SORTSET(S,ORDER) sorts a set S according to the order specified by ORDER. If elements of S are not present in ORDER they will be put at the end of R in the original order.
For matrices, SORTSET(S,ORDER) sorts each column of S, and for N-D arrays, SORTSET(S,ORDER) sorts the elements along the first non-singleton dimension of S.

SORTSET(S,ORDER,DIM) sorts along the dimension DIM.

[R, I, J] = SORTSET(S, ORDER) returns index vectors so that R equals S(I), and R(J) equals S. R, I, and J have the same size as S.

S and ORDER can be cell arrays of strings.

Examples:
% Sort numbers in a self-defined order: first 80-100, than reverse
% odd first, than rest
S = ceil(rand(1,10)*100) % 10 random numbers
ORDER = [80:100 99:-2:1 1:100] ;
R = sortset(S,ORDER)

% sort a list of words (with the word 'xxx' not in the list)
S = {'bb','a','dd','a','cc','dd','xxx','cc','e','bb'}
[R,I,J] = sortset(S, {'a','e','bb','dd','cc'})
isequal(R,S(I)), isequal(R(J),S)

% sort the list of words along columns
S = {'bb','a','dd','a','cc' ; 'dd','dd','cc','e','bb'}
R = sortset(S,{'a','e','bb','dd','cc'},2)

Notes:
- If an element occurs multiple times in ORDER, the first occurence is taken as its rank. For instance, when ORDER is [20 13 1 13] and S is [13 1] the result will be [13 1] and not [1 13].
- The functionality is similar to, but more convenient than something like:
S = {'bb','a','dd','a','cc','dd','dd','cc','e','bb'}
Ranks = [3 1 4 1 5 4 4 5 2 3] ;
[SRanks, si] = sort(Ranks) ; R2 = S(si)

引用格式

Jos (10584) (2025). SORTSET (v2.0) (https://ww2.mathworks.cn/matlabcentral/fileexchange/21762-sortset-v2-0), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R14
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0

(v2.0) changed towards similar functionality as SORT, checked textual errors