{"id":77,"date":"2007-02-16T17:31:56","date_gmt":"2007-02-16T22:31:56","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=77"},"modified":"2017-09-24T19:23:44","modified_gmt":"2017-09-25T00:23:44","slug":"reordering-arrays","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/02\/16\/reordering-arrays\/","title":{"rendered":"Reordering Arrays"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There are some cases, particularly simulations, in which we need to choose a few items from a finite list.  A friend mentioned\r\n         to me that he was doing a Monte Carlo simulation for a card game and wanted to deal out some card hands.  It was easier for\r\n         him to look at the hands if the cards were ordered so they were listed with the hands with the smallest high values were first\r\n         on the list.  As you can imagine, there are lots of ways to do this in MATLAB, and some take longer than others.  Also, there\r\n         are some variants on how to order the hands based on the lowest or intermediate cards, once the highest ones are duly sorted.\r\n         Why does this matter?  For my friend, the right ordering reduced his simulation times from 40 hours (so many hands!) to 15\r\n         hours, a 60% savings in time.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Deal Some Cards<\/a><\/li>\r\n         <li><a href=\"#2\">Reorder Cards by Lowest High Value<\/a><\/li>\r\n         <li><a href=\"#8\">Another Ordering<\/a><\/li>\r\n         <li><a href=\"#12\">Your Ordering Ideas<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Deal Some Cards<a name=\"1\"><\/a><\/h3>\r\n   <p>Let's find out all the card combinations taking 3 cards at a time and look at the first few.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hands3 = nchoosek(1:52,3);\r\nhands3(1:10,:)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1     2     3\r\n     1     2     4\r\n     1     2     5\r\n     1     2     6\r\n     1     2     7\r\n     1     2     8\r\n     1     2     9\r\n     1     2    10\r\n     1     2    11\r\n     1     2    12\r\n<\/pre><h3>Reorder Cards by Lowest High Value<a name=\"2\"><\/a><\/h3>\r\n   <p>Let's first try 3 different ways to reorder the cards, using<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/rot90.html\"><tt>rot90<\/tt><\/a>,\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fliplr.html\"><tt>fliplr<\/tt><\/a> and\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/flipud.html\"><tt>flipud<\/tt><\/a>,\r\n         <\/li>\r\n         <li>indexing\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">handsr = rot90(hands3,2);\r\nhandsr(1:8,:)<\/pre><pre style=\"font-style:oblique\">ans =\r\n    52    51    50\r\n    52    51    49\r\n    52    50    49\r\n    51    50    49\r\n    52    51    48\r\n    52    50    48\r\n    51    50    48\r\n    52    49    48\r\n<\/pre><p>These values start with the high ones, so let's transform our deck.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hands3t = 53-hands3;<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tic\r\nhandsr = rot90(hands3t,2);\r\ntoc\r\nhandsr(1:8,:)<\/pre><pre style=\"font-style:oblique\">Elapsed time is 0.000998 seconds.\r\nans =\r\n     1     2     3\r\n     1     2     4\r\n     1     3     4\r\n     2     3     4\r\n     1     2     5\r\n     1     3     5\r\n     2     3     5\r\n     1     4     5\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tic\r\nhandsff = flipud(fliplr(hands3t));\r\ntoc\r\nhandsff(1:8,:)<\/pre><pre style=\"font-style:oblique\">Elapsed time is 0.002005 seconds.\r\nans =\r\n     1     2     3\r\n     1     2     4\r\n     1     3     4\r\n     2     3     4\r\n     1     2     5\r\n     1     3     5\r\n     2     3     5\r\n     1     4     5\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tic\r\nhandsind = hands3t(end:-1:1,end:-1:1);\r\ntoc\r\nhandsind(1:8,:)<\/pre><pre style=\"font-style:oblique\">Elapsed time is 0.000973 seconds.\r\nans =\r\n     1     2     3\r\n     1     2     4\r\n     1     3     4\r\n     2     3     4\r\n     1     2     5\r\n     1     3     5\r\n     2     3     5\r\n     1     4     5\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sameOrder = isequal(handsr, handsff, handsind)<\/pre><pre style=\"font-style:oblique\">sameOrder =\r\n     1\r\n<\/pre><h3>Another Ordering<a name=\"8\"><\/a><\/h3>\r\n   <p>You can use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/sortrows.html\"><tt>sortrows<\/tt><\/a> to reorder the data as well.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tic\r\nhandsort = sortrows(hands3,3);\r\ntoc\r\nhandsort(1:8,:)\r\nisequal(handsort, handsr)<\/pre><pre style=\"font-style:oblique\">Elapsed time is 0.013555 seconds.\r\nans =\r\n     1     2     3\r\n     1     2     4\r\n     1     3     4\r\n     2     3     4\r\n     1     2     5\r\n     1     3     5\r\n     1     4     5\r\n     2     3     5\r\nans =\r\n     0\r\n<\/pre><p>Notice in this case that although the last column is sorted the same as the others, the first two columns come in a different\r\n      ordering.  Plus, I didn't have to do the first transformation of <tt>53-cardvalue<\/tt>.  It's not until the 7th row that we can see the difference.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[handsr(7:8,1:3) handsort(7:8,1:3)]<\/pre><pre style=\"font-style:oblique\">ans =\r\n     2     3     5     1     4     5\r\n     1     4     5     2     3     5\r\n<\/pre><p>How many rows differ?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sum(any(handsr-handsort,2))<\/pre><pre style=\"font-style:oblique\">ans =\r\n       21832\r\n<\/pre><p>Lots of them!<\/p>\r\n   <h3>Your Ordering Ideas<a name=\"12\"><\/a><\/h3>\r\n   <p>Do you need to order data for your work? What are some of the tips you can pass along?  Add them <a href=\"?p=77#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5d6a4f0f4fa849479462f09068fcc082() {\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='5d6a4f0f4fa849479462f09068fcc082 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5d6a4f0f4fa849479462f09068fcc082';\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_5d6a4f0f4fa849479462f09068fcc082()\"><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.4<br><\/p>\r\n<\/div>\r\n<!--\r\n5d6a4f0f4fa849479462f09068fcc082 ##### SOURCE BEGIN #####\r\n%% Reordering Arrays\r\n% There are some cases, particularly simulations, in which we need to\r\n% choose a few items from a finite list.  A friend mentioned to me that\r\n% he was doing a Monte Carlo simulation for a card game and wanted to deal\r\n% out some card hands.  It was easier for him to look at the hands if the\r\n% cards were ordered so they were listed with the hands with the smallest\r\n% high values were first on the list.  As you can imagine, there are lots\r\n% of ways to do this in MATLAB, and some take longer than others.  Also,\r\n% there are some variants on how to order the hands based on the lowest or\r\n% intermediate cards, once the highest ones are duly sorted.  Why does this\r\n% matter?  For my friend, the right ordering reduced his simulation times\r\n% from 40 hours (so many hands!) to 15 hours, a 60% savings in time.\r\n%% Deal Some Cards\r\n% Let's find out all the card combinations taking 3 cards at a time and\r\n% look at the first few.\r\nhands3 = nchoosek(1:52,3);\r\nhands3(1:10,:)\r\n%% Reorder Cards by Lowest High Value\r\n% Let's first try 3 different ways to reorder the cards, using\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/rot90.html |rot90|>,\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fliplr.html |fliplr|> and\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/flipud.html |flipud|>,\r\n% * indexing\r\nhandsr = rot90(hands3,2);\r\nhandsr(1:8,:)\r\n%%\r\n% These values start with the high ones, so let's transform our deck.\r\nhands3t = 53-hands3;\r\n%%\r\ntic\r\nhandsr = rot90(hands3t,2);\r\ntoc\r\nhandsr(1:8,:)\r\n%%\r\ntic\r\nhandsff = flipud(fliplr(hands3t));\r\ntoc\r\nhandsff(1:8,:)\r\n%%\r\ntic\r\nhandsind = hands3t(end:-1:1,end:-1:1);\r\ntoc\r\nhandsind(1:8,:)\r\n%%\r\nsameOrder = isequal(handsr, handsff, handsind)\r\n%%  Another Ordering\r\n% You can use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/sortrows.html |sortrows|>\r\n% to reorder the data as well.\r\ntic\r\nhandsort = sortrows(hands3,3);\r\ntoc\r\nhandsort(1:8,:)\r\nisequal(handsort, handsr)\r\n%% \r\n% Notice in this case that although the last column is sorted the same as\r\n% the others, the first two columns come in a different ordering.  Plus, I\r\n% didn't have to do the first transformation of |53-cardvalue|.  It's not\r\n% until the 7th row that we can see the difference.\r\n[handsr(7:8,1:3) handsort(7:8,1:3)]\r\n%%\r\n% How many rows differ?\r\nsum(any(handsr-handsort,2))\r\n%%\r\n% Lots of them!\r\n%% Your Ordering Ideas\r\n% Do you need to order data for your work? What are some of the tips you\r\n% can pass along?  Add them <?p=77#respond here>.\r\n\r\n##### SOURCE END ##### 5d6a4f0f4fa849479462f09068fcc082\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There are some cases, particularly simulations, in which we need to choose a few items from a finite list.  A friend mentioned\r\n         to me that he was doing a Monte Carlo... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/02\/16\/reordering-arrays\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/77"}],"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=77"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":2444,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/77\/revisions\/2444"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}