{"id":104,"date":"2007-08-21T10:55:12","date_gmt":"2007-08-21T15:55:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/08\/21\/reversal-of-a-sort\/"},"modified":"2016-08-03T14:46:30","modified_gmt":"2016-08-03T19:46:30","slug":"reversal-of-a-sort","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/08\/21\/reversal-of-a-sort\/","title":{"rendered":"Reversal of a sort"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>We can sometimes be motivated to reverse things in Boston.  And I occasionally get asked how to reverse the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/sort.html\"><tt>sort<\/tt><\/a> direction from MATLAB.  So instead of sorting <tt>A<\/tt> and then having <tt>B<\/tt> follow the new order, let's undo a sorting operation, and in a way that multiple vectors could benefit, if necessary.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Forward Sorting<\/a><\/li>\r\n         <li><a href=\"#7\">Reverse Sorting<\/a><\/li>\r\n         <li><a href=\"#10\">Help Me Here<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Forward Sorting<a name=\"1\"><\/a><\/h3>\r\n   <p>To sort multiple additional vectors in the same way as an initial one, we can easily take advantage of the sort index.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = [1 8 3 17 0 4 7];\r\n[sortA, ind] = sort(A);\r\nB = [2 5 6 1 9 3 8];\r\nsortBbyA = B(ind);\r\nsortB = sort(B);<\/pre><p>Here are the indices required to rearrange <tt>A<\/tt> into <tt>sortA<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ind<\/pre><pre style=\"font-style:oblique\">ind =\r\n     5     1     3     6     7     2     4\r\n<\/pre><p>And here's a comparison of the original vectors and the ones sorted by the order in <tt>A<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[A;B]\r\n[sortA;sortBbyA]<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1     8     3    17     0     4     7\r\n     2     5     6     1     9     3     8\r\nans =\r\n     0     1     3     4     7     8    17\r\n     9     2     6     3     8     5     1\r\n<\/pre><p>You can see that each number in <tt>A<\/tt> still corresponds to the same value from <tt>B<\/tt> after each vector has been sorted based on <tt>A<\/tt>.\r\n   <\/p>\r\n   <p>Now let's have a look at the variants of the vector <tt>B<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[B;sortB;sortBbyA]<\/pre><pre style=\"font-style:oblique\">ans =\r\n     2     5     6     1     9     3     8\r\n     1     2     3     5     6     8     9\r\n     9     2     6     3     8     5     1\r\n<\/pre><p>You can see that sorting <tt>B<\/tt> according to <tt>A<\/tt> (the 3rd row) is distinct from sorting <tt>B<\/tt> directly (second row).\r\n   <\/p>\r\n   <h3>Reverse Sorting<a name=\"7\"><\/a><\/h3>\r\n   <p>Suppose instead, I want all my vectors to be sorted the same way that the original <tt>A<\/tt> is sorted.  I can still use the index from <tt>sort<\/tt> but in a different way.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">unsorted = 1:length(A);\r\nnewInd(ind) = unsorted<\/pre><pre style=\"font-style:oblique\">newInd =\r\n     2     6     3     7     1     4     5\r\n<\/pre><p>We are now in a position to undo the original sorting of <tt>A<\/tt> and apply the rearrange variants of <tt>B<\/tt> the same way.  First we'll work with the variant of <tt>B<\/tt> based on sorting from <tt>A<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">newA = sortA(newInd);\r\nnewBfromA = sortBbyA(newInd);\r\nisequal([newA;newBfromA],[A;B])<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><p>Now let's compare several variants: <tt>B<\/tt>, the regular sorting of <tt>B<\/tt>, <tt>B<\/tt> sorted by <tt>A<\/tt>, and the sorted <tt>B<\/tt> rearranged according to the reverse sorting of <tt>A<\/tt><\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">newB = sortB(newInd);\r\n[B;sortB;sortBbyA;newB]<\/pre><pre style=\"font-style:oblique\">ans =\r\n     2     5     6     1     9     3     8\r\n     1     2     3     5     6     8     9\r\n     9     2     6     3     8     5     1\r\n     2     8     3     9     1     5     6\r\n<\/pre><h3>Help Me Here<a name=\"10\"><\/a><\/h3>\r\n   <p>Now I've shown how to use the direct sorting indices of a vector to reverse sort another vector.  I can even vaguely recall\r\n      needing to do this once, but I can no longer remember why.  Do you use this construct? Can you tell me some applications?\r\n       I'd love to hear about them; please post <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=104#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_23a42ebc874249e98830f2aaaea358d8() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='23a42ebc874249e98830f2aaaea358d8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 23a42ebc874249e98830f2aaaea358d8';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_23a42ebc874249e98830f2aaaea358d8()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.5<br><\/p>\r\n<\/div>\r\n<!--\r\n23a42ebc874249e98830f2aaaea358d8 ##### SOURCE BEGIN #####\r\n%% Reversal of a sort\r\n% We can sometimes be motivated to \r\n% <http:\/\/www.reversingthecurse.org\/ reverse> things in Boston.  And I\r\n% occasionally get asked how to reverse the \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/sort.html |sort|> \r\n% direction from MATLAB.  So instead of sorting |A| and then having |B|\r\n% follow the new order, let's undo a sorting operation, and in a way that\r\n% multiple vectors could benefit, if necessary.\r\n%% Forward Sorting\r\n% To sort multiple additional vectors in the same way as an initial one,\r\n% we can easily take advantage of the sort index.\r\nA = [1 8 3 17 0 4 7];\r\n[sortA, ind] = sort(A);\r\nB = [2 5 6 1 9 3 8];\r\nsortBbyA = B(ind);\r\nsortB = sort(B);\r\n%%\r\n% Here are the indices required to rearrange |A| into |sortA|.\r\nind\r\n%%\r\n% And here's a comparison of the original vectors and the ones sorted by\r\n% the order in |A|.\r\n[A;B]\r\n[sortA;sortBbyA]\r\n%%\r\n% You can see that each number in |A| still\r\n% corresponds to the same value from |B| after each vector has been sorted\r\n% based on |A|.\r\n%%\r\n% Now let's have a look at the variants of the vector |B|.\r\n[B;sortB;sortBbyA]\r\n%%\r\n% You can see that sorting |B| according to |A| (the 3rd row) is distinct\r\n% from sorting |B| directly (second row).\r\n%% Reverse Sorting\r\n% Suppose instead, I want all my vectors to be sorted the same way that the\r\n% original |A| is sorted.  I can still use the index from |sort| but in a\r\n% different way.\r\nunsorted = 1:length(A);\r\nnewInd(ind) = unsorted\r\n%%\r\n% We are now in a position to undo the original sorting of |A| and apply\r\n% the rearrange variants of |B| the same way.  First we'll work with the\r\n% variant of |B| based on sorting from |A|.\r\nnewA = sortA(newInd);\r\nnewBfromA = sortBbyA(newInd);\r\nisequal([newA;newBfromA],[A;B])\r\n%%\r\n% Now let's compare several variants: |B|, the regular sorting of |B|, |B|\r\n% sorted by |A|, and the sorted |B| rearranged according to the reverse\r\n% sorting of |A|\r\nnewB = sortB(newInd);\r\n[B;sortB;sortBbyA;newB]\r\n%% Help Me Here\r\n% Now I've shown how to use the direct sorting indices of a vector to\r\n% reverse sort another vector.  I can even vaguely recall needing to do\r\n% this once, but I can no longer remember why.  Do you use this construct?\r\n% Can you tell me some applications?  I'd love to hear about them; please\r\n% post <https:\/\/blogs.mathworks.com\/loren\/?p=104#respond here>.\r\n\r\n##### SOURCE END ##### 23a42ebc874249e98830f2aaaea358d8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      We can sometimes be motivated to reverse things in Boston.  And I occasionally get asked how to reverse the sort direction from MATLAB.  So instead of sorting A and then having B follow... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/08\/21\/reversal-of-a-sort\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/104"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":1920,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/104\/revisions\/1920"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}