{"id":3369,"date":"2012-03-02T10:59:29","date_gmt":"2012-03-02T15:59:29","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3369"},"modified":"2012-03-02T10:59:29","modified_gmt":"2012-03-02T15:59:29","slug":"when-deal-doesnt-work-disperse-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/03\/02\/when-deal-doesnt-work-disperse-2\/","title":{"rendered":"When Deal doesn&#8217;t work, Disperse!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33866-disperse\">Disperse,<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/193789\">Sam Hallman<\/a>.\r\n   <\/p>\r\n   <p>When I first saw Sam's \"disperse\" function--with the tagline \"Assign elements of an input array to individual output variables\r\n      with a single function call--\" I thought, \"well, that's silly! Why not just use MATLAB's <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/deal.html\">deal<\/a> function?\" Deal <i>lives<\/i> to distribute inputs to outputs.\r\n   <\/p>\r\n   <p>But fortunately for me, I read on, and came to Sam's statement: \"DISPERSE is not the same as DEAL. For example, if A is a\r\n      vector then [a b c d] = disperse(A) performs a=A(1), b=A(2), c=A(3), and d=A(4), whereas [a b c d] = deal(A) performs a=A,\r\n      b=A, c=A, and d=A.\" That immediately had me thinking about all the many times over the years when I've looked to DEAL to do\r\n      something it wasn't designed to do, and ended up writing for-loops, or clumsy work-around code, instead. But no longer!\r\n   <\/p>\r\n   <p>Sam's code is terse, yet widely useful. It works beautifully on cell arrays:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">data = {<span style=\"color: #A020F0\">'one'<\/span> [2 3] <span style=\"color: #A020F0\">'four'<\/span> 5}\r\n[a,b,c,d] = disperse(data)<\/pre><pre style=\"font-style:oblique\">data = \r\n  Columns 1 through 3\r\n    'one'    [1x2 double]    'four'\r\n  Column 4\r\n    [5]\r\na =\r\none\r\nb =\r\n     2     3\r\nc =\r\nfour\r\nd =\r\n     5\r\n<\/pre><p>Compare that to:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[a,b,c,d] = deal(data)<\/pre><pre style=\"font-style:oblique\">a = \r\n  Columns 1 through 3\r\n    'one'    [1x2 double]    'four'\r\n  Column 4\r\n    [5]\r\nb = \r\n  Columns 1 through 3\r\n    'one'    [1x2 double]    'four'\r\n  Column 4\r\n    [5]\r\nc = \r\n  Columns 1 through 3\r\n    'one'    [1x2 double]    'four'\r\n  Column 4\r\n    [5]\r\nd = \r\n  Columns 1 through 3\r\n    'one'    [1x2 double]    'four'\r\n  Column 4\r\n    [5]\r\n<\/pre><p>DISPERSE works with the elements of structs, too, where a usage of DEAL would similarly assign the full structure to each\r\n      of the outputs:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = struct(<span style=\"color: #A020F0\">'strings'<\/span>, {<span style=\"color: #A020F0\">'sam'<\/span>,<span style=\"color: #A020F0\">'hello'<\/span>}, <span style=\"color: #A020F0\">'lengths'<\/span>, [3 5])\r\n[a b] = disperse(s)<\/pre><pre style=\"font-style:oblique\">s = \r\n1x2 struct array with fields:\r\n    strings\r\n    lengths\r\na = \r\n    strings: 'sam'\r\n    lengths: [3 5]\r\nb = \r\n    strings: 'hello'\r\n    lengths: [3 5]\r\n<\/pre><p>Note that we could workaround the cell problem with this approach:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[a,b,c,d] = deal(data{:})<\/pre><pre style=\"font-style:oblique\">a =\r\none\r\nb =\r\n     2     3\r\nc =\r\nfour\r\nd =\r\n     5\r\n<\/pre><p>And for structures, we could write something like this:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sc = num2cell(s);\r\n[a, b] = deal(sc{:})<\/pre><pre style=\"font-style:oblique\">a = \r\n    strings: 'sam'\r\n    lengths: [3 5]\r\nb = \r\n    strings: 'hello'\r\n    lengths: [3 5]\r\n<\/pre><p>But the simplified syntax and ease of use still recommends DISPERSE.<\/p>\r\n   <p>You can subset your data, extract (disperse) elements of an n-dimensional array, even extract individual color planes in an\r\n      RGB image:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">img = imread(<span style=\"color: #A020F0\">'peppers.png'<\/span>);\r\n[r g b] = disperse(img);\r\n<span style=\"color: #228B22\">% figure;<\/span>\r\n<span style=\"color: #228B22\">% subplot(2,2,1)<\/span>\r\n<span style=\"color: #228B22\">% imshow(img); title('RGB')<\/span>\r\n<span style=\"color: #228B22\">% subplot(2,2,2)<\/span>\r\n<span style=\"color: #228B22\">% imshow(r); title('Red')<\/span>\r\n<span style=\"color: #228B22\">% subplot(2,2,3)<\/span>\r\n<span style=\"color: #228B22\">% imshow(g); title('Green')<\/span>\r\n<span style=\"color: #228B22\">% subplot(2,2,4)<\/span>\r\n<span style=\"color: #228B22\">% imshow(b); title('Blue')<\/span><\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/disperseimage.png\"> <\/p>\r\n   <p>DISPERSE ships with an <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33866-disperse-disperse\/content\/GettingStarted.html\">html report<\/a>, generated with MATLAB's <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/ref\/publish.html\">PUBLISH<\/a> function, that shows lots of ways to use the function. It's thorough, and very helpful.\r\n   <\/p>\r\n   <p>As always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3369#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for Sam <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33866-disperse#comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_637bb3545f7b49b785fd151265bdb1c0() {\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='637bb3545f7b49b785fd151265bdb1c0 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 637bb3545f7b49b785fd151265bdb1c0';\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 = 'Robert Bemis';\r\n        copyright = 'Copyright 2012 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_637bb3545f7b49b785fd151265bdb1c0()\"><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.13<br><\/p>\r\n<\/div>\r\n<!--\r\n637bb3545f7b49b785fd151265bdb1c0 ##### SOURCE BEGIN #####\r\n%% When Deal doesn't work, Disperse!\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33866-disperse Disperse,> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/193789 Sam Hallman>.\r\n\r\n%%\r\n% When I first saw Sam's \"disperse\" functionREPLACE_WITH_DASH_DASHwith the\r\n% tagline \"Assign elements of an input array to individual\r\n% output variables with a single function callREPLACE_WITH_DASH_DASH\" I thought,\r\n% \"well, that's silly! Why not just use MATLAB's\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/deal.html deal>\r\n% function?\" Deal _lives_ to distribute inputs to outputs.\r\n\r\n%%\r\n% But fortunately for me, I read on, and came to Sam's\r\n% statement: \"DISPERSE is not the same as DEAL. For example,\r\n% if A is a vector then [a b c d] = disperse(A) performs\r\n% a=A(1), b=A(2), c=A(3), and d=A(4), whereas [a b c d] =\r\n% deal(A) performs a=A, b=A, c=A, and d=A.\" That immediately\r\n% had me thinking about all the many times over the years when I've looked to\r\n% DEAL to do something it wasn't designed to do, and ended\r\n% up writing for-loops, or clumsy work-around code, instead. But no longer!\r\n\r\n%%\r\n% Sam's code is terse, yet widely useful. It works\r\n% beautifully on cell arrays:\r\n\r\ndata = {'one' [2 3] 'four' 5}\r\n[a,b,c,d] = disperse(data)\r\n\r\n%%\r\n% Compare that to:\r\n[a,b,c,d] = deal(data)\r\n\r\n%%\r\n% DISPERSE works with the elements of structs, too, where a\r\n% usage of DEAL would similarly assign the full structure to each of\r\n% the outputs:\r\ns = struct('strings', {'sam','hello'}, 'lengths', [3 5])\r\n[a b] = disperse(s)\r\n\r\n%%\r\n% Note that we could workaround the cell problem with this approach:\r\n[a,b,c,d] = deal(data{:})\r\n\r\n%%\r\n% And for structures, we could write something like this:\r\nsc = num2cell(s);\r\n[a, b] = deal(sc{:})\r\n\r\n%%\r\n% But the simplified syntax and ease of use still recommends DISPERSE.\r\n\r\n%%\r\n% You can subset your data, extract (disperse) elements of\r\n% an n-dimensional array, even extract individual color\r\n% planes in an RGB image:\r\nimg = imread('peppers.png');\r\n[r g b] = disperse(img);\r\n% figure;\r\n% subplot(2,2,1)\r\n% imshow(img); title('RGB')\r\n% subplot(2,2,2)\r\n% imshow(r); title('Red')\r\n% subplot(2,2,3)\r\n% imshow(g); title('Green')\r\n% subplot(2,2,4)\r\n% imshow(b); title('Blue')\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/disperseimage.png>>\r\n% \r\n\r\n%% \r\n% DISPERSE ships with an <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33866-disperse-disperse\/content\/GettingStarted.html html report>, \r\n% generated with MATLAB's <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/ref\/publish.html PUBLISH> function, that shows lots of\r\n% ways to use the function. It's thorough, and very helpful.\r\n\r\n%% \r\n% As always, <https:\/\/blogs.mathworks.com\/pick\/?p=3369#respond comments to this blog post> are welcome. Or leave a\r\n% comment for Sam\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33866-disperse#comments here>.\r\n\r\n##### SOURCE END ##### 637bb3545f7b49b785fd151265bdb1c0\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is Disperse, by Sam Hallman.\r\n   \r\n   When I first saw Sam's \"disperse\" function--with the tagline \"Assign elements of an input array to individual output variables\r\n... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/03\/02\/when-deal-doesnt-work-disperse-2\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3369"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3369"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3369\/revisions"}],"predecessor-version":[{"id":3378,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3369\/revisions\/3378"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}