This demo shows how to use indices of sorted sets and how to recover the unsorted set from the sorted one.
% A set of values is gven; each value has its index k = 1:N Mu = [10, 23, 15, 27, 12, 11]; names = {'A','B','C','D','E','F'}; % Sort and recover unsorted set using an index MuSotred = Mu(idx) MuRecov(idx) = MuSotred Compare = Mu == MuRecov % Useful variables N = length(Mu); MaxMu = max(Mu); % Denote by idx the indices of the sorted set [tmp idx] = sort(Mu); % Show 2 bar plots: unsorted and sorted subplot(1, 2, 1); hold on bar(Mu); text(1:N, MaxMu*ones(1,N)-1, names); axis('tight'); ylabel('value'); xlabel('indices of unsorted set'); title('unsorted set'); subplot(1, 2, 2); hold on bar(Mu(idx)); text(1:N, MaxMu*ones(1,N)-1, names(idx)); axis('tight'); ylabel('value'); xlabel('indices of sorted set'); title('sorted set'); % select some bar on the right plot [p, tmp] = ginput(1); % p p = round(p); %show selected bars subplot(1, 2, 1); bar(idx(p),Mu(idx(p)), 'r'); hold off subplot(1, 2, 2); bar(p,Mu(idx(p)), 'r'); hold off
MuSotred = 10 11 12 15 23 27 MuRecov = 10 23 15 27 12 11 Compare = 1 1 1 1 1 1
<<img/demo_indexation_01.png>> return % and note that Mu idx p=5 Mu (idx(p)) k = idx(p) [tmp inv] = sort(idx) inv(idx) idx(inv) idx inv