{"id":247,"date":"2010-09-30T13:08:26","date_gmt":"2010-09-30T13:08:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/09\/30\/rearranging-data\/"},"modified":"2016-08-04T09:22:22","modified_gmt":"2016-08-04T14:22:22","slug":"rearranging-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/09\/30\/rearranging-data\/","title":{"rendered":"Rearranging Data"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I can think of a lot of functions that rearrange data in MATLAB.  I've long suspected that not all of these are well-known,\r\n         though some are clearly daily tools.  Maybe it's time to be sure they get exposure.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">My List of Functions for Rearranging Data<\/a><\/li>\r\n         <li><a href=\"#2\">Frequency of Use?<\/a><\/li>\r\n         <li><a href=\"#8\">What Do You Use?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>My List of Functions for Rearranging Data<a name=\"1\"><\/a><\/h3>\r\n   <p>Here's my <i>off the top of my head<\/i> incomplete list.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/reshape.html\"><tt>reshape<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/arithmeticoperators.html\"><tt>ctranspose, transpose<\/tt><\/a>, i.e., <tt>'<\/tt>, <tt>.'<\/tt><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/repmat.html\"><tt>repmat<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/squeeze.html\"><tt>squeeze<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/permute.html\"><tt>permute<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/ipermute.html\"><tt>ipermute<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/circshift.html\"><tt>circshift<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/randperm.html\"><tt>randperm<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/kron.html\"><tt>kron<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/math\/f1-85462.html\">indexing<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Frequency of Use?<a name=\"2\"><\/a><\/h3>\r\n   <p>My guess is that <tt>circshift<\/tt> is one on the list that gets used least often.  It's called a circular shift because elements that fall off at one end appear\r\n      at the other end, wrapping around the values. Let's play with it to see what it can do.  I'll use unique numbers in the sample\r\n      matrix so we can follow them around.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = reshape(1:16,4,4)'<\/pre><pre style=\"font-style:oblique\">A =\r\n     1     2     3     4\r\n     5     6     7     8\r\n     9    10    11    12\r\n    13    14    15    16\r\n<\/pre><p>From the help, I get to shift each dimension \"up\" or \"down\".  Let's first just shift values in each column down by 2.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Adown2 = circshift(A,2)<\/pre><pre style=\"font-style:oblique\">Adown2 =\r\n     9    10    11    12\r\n    13    14    15    16\r\n     1     2     3     4\r\n     5     6     7     8\r\n<\/pre><p>Now let's shift just row values - to the right by 3.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Aright3 = circshift(A,[0 3])<\/pre><pre style=\"font-style:oblique\">Aright3 =\r\n     2     3     4     1\r\n     6     7     8     5\r\n    10    11    12     9\r\n    14    15    16    13\r\n<\/pre><p>How about shifting left by 1?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Aleft1 = circshift(A,[0 -1])<\/pre><pre style=\"font-style:oblique\">Aleft1 =\r\n     2     3     4     1\r\n     6     7     8     5\r\n    10    11    12     9\r\n    14    15    16    13\r\n<\/pre><p>We can see that shifting left by 1 is the same as shifting right by 3 when the number of columns is 4.<\/p>\r\n   <p>I can do a combination shift, with rows <b>and<\/b> columns.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Ad2l1 = circshift(A,[2 -1])<\/pre><pre style=\"font-style:oblique\">Ad2l1 =\r\n    10    11    12     9\r\n    14    15    16    13\r\n     2     3     4     1\r\n     6     7     8     5\r\n<\/pre><h3>What Do You Use?<a name=\"8\"><\/a><\/h3>\r\n   <p>What functions or techniques do you use to rearrange your data most often?  Do you have a favorite function in this category\r\n      that I didn't list?\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_e6da1991b3f044adb6ab9d02f53db20d() {\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='e6da1991b3f044adb6ab9d02f53db20d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e6da1991b3f044adb6ab9d02f53db20d';\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 2010 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_e6da1991b3f044adb6ab9d02f53db20d()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\ne6da1991b3f044adb6ab9d02f53db20d ##### SOURCE BEGIN #####\r\n%% Rearranging Data\r\n% I can think of a lot of functions that rearrange data in MATLAB.  I've\r\n% long suspected that not all of these are well-known, though some are\r\n% clearly daily tools.  Maybe it's time to be sure they get exposure.\r\n%% My List of Functions for Rearranging Data\r\n% Here's my _off the top of my head_ incomplete list.\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/reshape.html\r\n% |reshape|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/arithmeticoperators.html\r\n% |ctranspose, transpose|>, i.e., |'|, |.'|\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/repmat.html\r\n% |repmat|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/squeeze.html\r\n% |squeeze|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/permute.html\r\n% |permute|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/ipermute.html\r\n% |ipermute|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/circshift.html\r\n% |circshift|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/randperm.html\r\n% |randperm|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/kron.html\r\n% |kron|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/math\/f1-85462.html indexing>\r\n%% Frequency of Use?\r\n% My guess is that |circshift| is one on the list that gets used least\r\n% often.  It's called a circular shift because elements that fall off at\r\n% one end appear at the other end, wrapping around the values. Let's play\r\n% with it to see what it can do.  I'll use unique numbers in the sample\r\n% matrix so we can follow them around.\r\nA = reshape(1:16,4,4)'\r\n%%\r\n% From the help, I get to shift each dimension \"up\" or \"down\".  Let's first\r\n% just shift values in each column down by 2.\r\nAdown2 = circshift(A,2)\r\n%%\r\n% Now let's shift just row values - to the right by 3.\r\nAright3 = circshift(A,[0 3])\r\n%%\r\n% How about shifting left by 1?\r\nAleft1 = circshift(A,[0 -1])\r\n%%\r\n% We can see that shifting left by 1 is the same as shifting right by 3\r\n% when the number of columns is 4.\r\n%%\r\n% I can do a combination shift, with rows *and* columns.\r\nAd2l1 = circshift(A,[2 -1])\r\n%% What Do You Use?\r\n% What functions or techniques do you use to rearrange your data most\r\n% often?  Do you have a favorite function in this category that I didn't\r\n% list? Let me know <https:\/\/blogs.mathworks.com\/?p=247#respond here>.\r\n\r\n\r\n##### SOURCE END ##### e6da1991b3f044adb6ab9d02f53db20d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I can think of a lot of functions that rearrange data in MATLAB.  I've long suspected that not all of these are well-known,\r\n         though some are clearly daily tools.  Maybe... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/09\/30\/rearranging-data\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[39,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/247"}],"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=247"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":1977,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/247\/revisions\/1977"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}