{"id":5895,"date":"2015-03-20T09:00:38","date_gmt":"2015-03-20T13:00:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5895"},"modified":"2015-03-17T11:04:00","modified_gmt":"2015-03-17T15:04:00","slug":"expand","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/03\/20\/expand\/","title":{"rendered":"Expand"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24536-expand\">Expand<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/688530\">Matt Fig<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Background<\/a><\/li>\r\n         <li><a href=\"#2\">Expand<\/a><\/li>\r\n         <li><a href=\"#4\">Kron<\/a><\/li>\r\n         <li><a href=\"#5\">R2015a Introduces repelem<\/a><\/li>\r\n         <li><a href=\"#8\">R2015a Other<\/a><\/li>\r\n         <li><a href=\"#9\">Comments<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Background<a name=\"1\"><\/a><\/h3>\r\n   <p>Matt Fig is one of MATLAB Central's top contributors.  Even though he's been on hiatus for over two years, he's still the\r\n      10th highest contributor on Answers and the 14th most downloaded author on the File Exchange.\r\n   <\/p>\r\n   <h3>Expand<a name=\"2\"><\/a><\/h3>\r\n   <p>My pick today is a contribution of Matt's that made an appearance somewhere in the code base for my Master's thesis.  I was\r\n      working with large 3-dimensional CT images and computation speed was important. Here's what it does:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = magic(3)\r\nxexpanded = expand(x,[3 2])<\/pre><pre style=\"font-style:oblique\">x =\r\n     8     1     6\r\n     3     5     7\r\n     4     9     2\r\nxexpanded =\r\n     8     8     1     1     6     6\r\n     8     8     1     1     6     6\r\n     8     8     1     1     6     6\r\n     3     3     5     5     7     7\r\n     3     3     5     5     7     7\r\n     3     3     5     5     7     7\r\n     4     4     9     9     2     2\r\n     4     4     9     9     2     2\r\n     4     4     9     9     2     2\r\n<\/pre><p>As you can see <i>x<\/i> has been expanded thrice along rows and twice along columns ([3 2]).  Expand also works along N-dimensional arrays but does\r\n      have a limitation that the expansion must match the number of dimensions.  This can be circumvented with a second call to\r\n      <tt>repmat<\/tt> to expand along higher dimensions.\r\n   <\/p>\r\n   <h3>Kron<a name=\"4\"><\/a><\/h3>\r\n   <p>The 2-dimensional expand operation has always been possible with <a href=\"\"><tt>kron<\/tt><\/a>, but it requires creating a ones matrix and, at least back in my grad school era, was slower than <tt>expand<\/tt>.  Since I was working with 3-dimensional images, this was not an option.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xkron = kron(x,ones(3,2))<\/pre><pre style=\"font-style:oblique\">xkron =\r\n     8     8     1     1     6     6\r\n     8     8     1     1     6     6\r\n     8     8     1     1     6     6\r\n     3     3     5     5     7     7\r\n     3     3     5     5     7     7\r\n     3     3     5     5     7     7\r\n     4     4     9     9     2     2\r\n     4     4     9     9     2     2\r\n     4     4     9     9     2     2\r\n<\/pre><h3>R2015a Introduces repelem<a name=\"5\"><\/a><\/h3>\r\n   <p>Starting in R2015a, which shipped the first week of March, there is a new function that does the same thing as expand and\r\n      more built in to MATLAB: meet <a href=\"\"><tt>repelem<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xrep = repelem(x,3,2)<\/pre><pre style=\"font-style:oblique\">xrep =\r\n     8     8     1     1     6     6\r\n     8     8     1     1     6     6\r\n     8     8     1     1     6     6\r\n     3     3     5     5     7     7\r\n     3     3     5     5     7     7\r\n     3     3     5     5     7     7\r\n     4     4     9     9     2     2\r\n     4     4     9     9     2     2\r\n     4     4     9     9     2     2\r\n<\/pre><p>And into three dimensions:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xrep3 = repelem(x,3,1,2)<\/pre><pre style=\"font-style:oblique\">xrep3(:,:,1) =\r\n     8     1     6\r\n     8     1     6\r\n     8     1     6\r\n     3     5     7\r\n     3     5     7\r\n     3     5     7\r\n     4     9     2\r\n     4     9     2\r\n     4     9     2\r\nxrep3(:,:,2) =\r\n     8     1     6\r\n     8     1     6\r\n     8     1     6\r\n     3     5     7\r\n     3     5     7\r\n     3     5     7\r\n     4     9     2\r\n     4     9     2\r\n     4     9     2\r\n<\/pre><p><tt>repelem<\/tt> on a vector can also vary the number of repeats, very nice for building up a vector or <a href=\"http:\/\/en.wikipedia.org\/wiki\/Run-length_encoding\">run length encoding<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">v123 = repelem([2 4 7],1:3)<\/pre><pre style=\"font-style:oblique\">v123 =\r\n     2     4     4     7     7     7\r\n<\/pre><h3>R2015a Other<a name=\"8\"><\/a><\/h3>\r\n   <p>Browsing through the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html\">release notes<\/a> for R2015a and having had the opportunity to beat on the R2015a Prerelease, here are a few of my other favorite new features:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"\"><tt>discretize<\/tt><\/a> - similar to <tt>histcounts<\/tt> or <tt>histc<\/tt> but skips the histogram counting part.\r\n         <\/li>\r\n         <li>Array Size Limitation - Helps accidentally creating large arrays that would cause you to run out of RAM.<\/li>\r\n         <li>Tab Complete for Object Authoring - Finally!<\/li>\r\n         <li><a href=\"\"><tt>ismembertol<\/tt><\/a> and <a href=\"\"><tt>uniquetol<\/tt><\/a> - ismember and unique with tolerance; these will save me a few calls to <tt>discretize<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Comments<a name=\"9\"><\/a><\/h3>\r\n   <p>Thanks Matt for providing the <tt>expand<\/tt> functionality; it has been very useful for nearly 6 years!\r\n   <\/p>\r\n   <p>Give <tt>expand<\/tt>, <tt>repelem<\/tt>, and R2015a a try and let us know what you think about any of them <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5895#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24536-expand#comments\">comment<\/a> for Matt.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0d108546a6bf4c06bd7bcaff040100a7() {\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='0d108546a6bf4c06bd7bcaff040100a7 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0d108546a6bf4c06bd7bcaff040100a7';\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 = 'Sean de Wolski';\r\n        copyright = 'Copyright 2015 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_0d108546a6bf4c06bd7bcaff040100a7()\"><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; R2015a<br><\/p>\r\n<\/div>\r\n<!--\r\n0d108546a6bf4c06bd7bcaff040100a7 ##### SOURCE BEGIN #####\r\n%% Expand\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24536-expand Expand> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/688530 Matt Fig>.\r\n% \r\n\r\n%% Background\r\n%\r\n% Matt Fig is one of MATLAB Central's top contributors.  Even though\r\n% he's been on hiatus for over two years, he's still the 10th highest\r\n% contributor on Answers and the 14th most downloaded author on the File\r\n% Exchange.\r\n%\r\n%% Expand\r\n% My pick today is a contribution of Matt's that made an appearance\r\n% somewhere in the code base for my Master's thesis.  I was working with\r\n% large 3-dimensional CT images and computation speed was important.\r\n% Here's what it does:\r\n\r\nx = magic(3)\r\nxexpanded = expand(x,[3 2])\r\n\r\n%%\r\n% As you can see _x_ has been expanded thrice along rows and twice along\r\n% columns ([3 2]).  Expand also works along N-dimensional arrays but\r\n% does have a limitation that the expansion must match the number of\r\n% dimensions.  This can be circumvented with a second call to |repmat| to\r\n% expand along higher dimensions.\r\n\r\n%% Kron\r\n% The 2-dimensional expand operation has always been possible with\r\n% <\r\n% |kron|>, but it requires creating a ones matrix and, at least back\r\n% in my grad school era, was slower than |expand|.  Since I was working\r\n% with 3-dimensional images, this was not an option.\r\n\r\nxkron = kron(x,ones(3,2))\r\n\r\n%% R2015a Introduces repelem\r\n% Starting in R2015a, which shipped the first week of March, there is a new\r\n% function that does the same thing as expand and more built in to MATLAB:\r\n% meet\r\n% <\r\n% |repelem|>.\r\n\r\nxrep = repelem(x,3,2)\r\n\r\n%%\r\n% And into three dimensions:\r\n\r\nxrep3 = repelem(x,3,1,2)\r\n\r\n%%\r\n% |repelem| on a vector can also vary the number of repeats, very nice for\r\n% building up a vector or \r\n% <http:\/\/en.wikipedia.org\/wiki\/Run-length_encoding run length encoding>.\r\n\r\nv123 = repelem([2 4 7],1:3)\r\n\r\n%% R2015a Other\r\n% Browsing through the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html release notes>\r\n% for R2015a and having had the opportunity to beat on the R2015a\r\n% Prerelease, here are a few of my other favorite new features:\r\n%\r\n% * <\r\n% |discretize|> - similar to |histcounts| or |histc| but skips the\r\n% histogram counting part.\r\n% * Array Size Limitation - Helps accidentally creating large arrays that\r\n% would cause you to run out of RAM.\r\n% * Tab Complete for Object Authoring - Finally!\r\n% * <\r\n% |ismembertol|> and\r\n% <\r\n% |uniquetol|> - ismember and unique with tolerance; these will save me a\r\n% few calls to |discretize|.\r\n%\r\n\r\n\r\n%% Comments\r\n% \r\n% Thanks Matt for providing the |expand| functionality; it has been very\r\n% useful for nearly 6 years!\r\n%\r\n% Give |expand|, |repelem|, and R2015a a try and let us know what you think\r\n% about any of them\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5895#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24536-expand#comments\r\n% comment> for Matt.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 0d108546a6bf4c06bd7bcaff040100a7\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Sean's pick this week is Expand by Matt Fig.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n         Background\r\n ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/03\/20\/expand\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,16,28],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5895"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5895"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5895\/revisions"}],"predecessor-version":[{"id":5897,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5895\/revisions\/5897"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}