How can I use the returned sort index from the sort() function on a multidimensional array?

According to the documentation:
[B,I] = sort(___) also returns a collection of index vectors for any of the previous syntaxes. I is the same size as A and describes the arrangement of the elements of A into B along the sorted dimension. For example, if A is a vector, then B = A(I).
However, if A is not a vector, then B ~= A(I) since the sort index is returned as a collection of index vectors oriented along the same dimension that sort operates on rather than a linear index.
Is there a function to convert such a sort index into a linear index array, say f(I), such that B = A(f(I)), where f(x) would be similar in theory to sub2index() with the other dimensions inferred from the size of the sort index?

답변 (1개)

Matt J
Matt J 2023년 11월 30일
편집: Matt J 2023년 11월 30일
The easier way would be to use the attached modification of sort() which will simply return the output indices as lienar indices.
Example:
A=randi(100,5),
A = 5×5
24 33 22 38 11 56 1 48 44 2 90 79 76 97 94 32 18 91 67 73 46 66 72 47 71
[B1,I]=sortlidx(A,2);
B1,
B1 = 5×5
11 22 24 33 38 1 2 44 48 56 76 79 90 94 97 18 32 67 73 91 46 47 66 71 72
B2=A(I)
B2 = 5×5
11 22 24 33 38 1 2 44 48 56 76 79 90 94 97 18 32 67 73 91 46 47 66 71 72

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2023년 11월 30일

편집:

2023년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by