title: "Desort" a vector (undo a sorting)
body: <p>In Matlab, <code>sort</code> returns both the sorted vector and an index vector showing which vector element has been moved where:</p>
  
  <pre><code>[v, ix] = sort(u);
  </code></pre>
  
  <p>Here, <code>v</code> is a vector containing all the elements of <code>u</code>, but sorted. <code>ix</code> is a vector showing the original position of each element of <code>v</code> in <code>u</code>. Using Matlab's syntax, <code>u(ix) == v</code>.</p>
  
  <p><strong>My question:</strong> How do I obtain <code>u</code> from <code>v</code> and <code>ix</code>?</p>
  
  <p>Of course, I could simply use:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But I am having this tip-of-the-tongue feeling that there is a more elegant, single-statement, vectorized way of doing this.</p>
  
  <hr>
  
  <p><em>If you are wondering why one would need to do this instead of just using <code>u</code></em>: 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.</p>