Inverse of sorting arrangement
Show older comments
[B,I] = sort(A) output is such that B = A(I). What would be the most efficient way to rather get B and J, such that A = B(J)?
Accepted Answer
More Answers (2)
If A has distinct elements such as
A = rand(1,10)
a single line command is
[B,~,J] = unique(A);
Check
J
B(J)
1 Comment
Bruno Luong
on 27 Jun 2023
If A has non distinct element, then the above method gives B that is shorter than A (and B has distinct elements) and
B(J) = A
still hold.
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!