In Matlab, sort
returns both the sorted vector and an index vector showing which vector element has been moved where:
[v, ix] = sort(u);
Here, v
is a vector containing all the elements of u
, but sorted. ix
is a vector showing the original position of each element of v
in u
. Using Matlab's syntax, u(ix) == v
.
My question: How do I obtain u
from v
and ix
?
Of course, I could simply use:
w = zero(size(v));
for i = 1:length(v)
w(ix(i)) = v(i)
end
if nnz(w == u) == length(u)
print('Success!');
else
print('Failed!');
end
But I am having this tip-of-the-tongue feeling that there is a more elegant, single-statement, vectorized way of doing this.
If you are wondering why one would need to do this instead of just using u
: I was trying to implement the Benjamini-Hochberg procedure which adjusts each element of the vector based on its position after sorting, but recovering the original order after adjusting was important for me.