Thursday 5 June 2014

Getting the sort ordering


I wish I'd known MATLAB had... [Y,I] = SORT(X)

  • Sort has two outputs! The second output is the indices of the elements, i.e. the re-ordering vector
  • Y is the same as X(I)
  • Very handy if you want to sort a matrix by one column [~,I] = SORT(M(:,1)); % sort M by its first column Y = M(I,:);

No comments:

Post a Comment