No BSD License  

Highlights from
matchsort

Be the first to rate this file! 2 Downloads (last 30 days) File Size: 3.45 KB File ID: #19422

matchsort

by Garrett Euler

 

01 Apr 2008 (Updated 04 Apr 2008)

Replicate a sort operation using the returned permutation indices

| Watch this File

File Information
Description

Replicates a previous sort operation using the permutation matrix from that previous sort. Useful for sorting multiple matrices in parallel. If no dimension argument is supplied, matchsort will operate on the first non-singleton dimension just like sort does. Second output is the permutation matrix transformed into the corresponding matrix of linear indices.

Usage: y=matchsort(x,i)
       y=matchsort(x,i,dim)
       [y,li]=matchsort(x,i,dim)

Example:
     
Sort a and then sort the elements of b,c exactly the same way.
      b=1./a
      c=-a
      [d,i]=sort(a)
      e=matchsort(b,i)
      f=matchsort(c,i)
      isequal(d,1./e,-f)

Acknowledgements
This submission has inspired the following:
SORTLIND (v2.1)
MATLAB release MATLAB 7.5 (R2007b)
Other requirements Not tested on pre-7 Matlab.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
02 Apr 2008 John D'Errico

I'm confused. Exactly what does this function do that the trivial code below does not do?

[x,tags] = sort(x);
y = y(tags);

02 Apr 2008 Garrett Euler

Sure its trivial if x is a vector...not as much if x is a n-d matrix sorted down an arbitrary dimension. Note that your example won't work for anything other than a vector. This function is useful for parallel sorting of matrices of 2+ dimensions.

A clearer example:
a=magic(10);
b=1./a
c=-a
[d,i]=sort(a,2)
e=matchsort(b,i,2)
f=matchsort(c,i,2)
isequal(d,1./e,-f)

03 Apr 2008 John D'Errico

I''ll concede that as a valid use.

Please login to add a comment or rating.
Updates
03 Apr 2008

Updated submat_noeval subfunction.

03 Apr 2008

For some reason I thought reshaping was necessary - I've removed that step now as it did nothing. Thanks to Jos's sortlind function (file id 19443) for pointing that out!

04 Apr 2008

Update didn't update last update.

Tag Activity for this File
Tag Applied By Date/Time
matrices Garrett Euler 22 Oct 2008 09:56:11
sort Garrett Euler 22 Oct 2008 09:56:11
replicate Garrett Euler 22 Oct 2008 09:56:11
matchsort Garrett Euler 22 Oct 2008 09:56:11
matrix manipulation Garrett Euler 22 Oct 2008 09:56:11

Contact us at files@mathworks.com