{"id":123,"date":"2008-01-24T10:55:10","date_gmt":"2008-01-24T15:55:10","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/01\/24\/deal-or-no-deal\/"},"modified":"2016-08-01T10:19:55","modified_gmt":"2016-08-01T15:19:55","slug":"deal-or-no-deal","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/01\/24\/deal-or-no-deal\/","title":{"rendered":"Deal or No Deal"},"content":{"rendered":"<div class=\"content\">\n<p>This post continues in the theme from my last post, where people routinely come to me on a topic. Today I want to distinguish<br \/>\nbetween indexed assignment, where you can take advantage of scalar expansion, and assignment to several output arrays, often<br \/>\narising from a comma-separated list from cell or struct arrays.<\/p>\n<p>&nbsp;<\/p>\n<h3>Contents<\/h3>\n<div>\n<ul>\n<li><a href=\"#1\">First Explore Cell Arrays<\/a><\/li>\n<li><a href=\"#9\">Now Explore structs<\/a><\/li>\n<li><a href=\"#14\">References<\/a><\/li>\n<li><a href=\"#15\">Why Do You Use deal?<\/a><\/li>\n<\/ul>\n<\/div>\n<h3>First Explore Cell Arrays<a name=\"1\"><\/a><\/h3>\n<p>Let's first explore some aspects of creating and populating the contents of a cell array. I first initialize 2 arrays, each<br \/>\nof length 3.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">a = {1 magic(3) <span style=\"color: #a020f0;\">'hello'<\/span>};\r\nd = {<span style=\"color: #a020f0;\">'hi'<\/span> 17 pi};<\/pre>\n<p>Next I initialize another cell array to be the same as <tt>a<\/tt>.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">c = a;<\/pre>\n<p>Now I attempt to change the contents of all the cells in <tt>c<\/tt> by assigning a new value that I want to show up in each cell.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #0000ff;\">try<\/span>\r\n    [c{:}] = <span style=\"color: #a020f0;\">'aloha'<\/span>; <span style=\"color: #228b22;\">% error unless c is a scalar<\/span>\r\n<span style=\"color: #0000ff;\">catch<\/span> cellE\r\n    disp(cellE.message)\r\n<span style=\"color: #0000ff;\">end<\/span><\/pre>\n<pre style=\"font-style: oblique;\">Too many output arguments.\r\n<\/pre>\n<p>As you can see, this didn't work. The reason is that I have 3 entities on the left-hand side, because <tt>c<\/tt> is a cell array of length 3 and I used the comma-separated list notion. MATLAB doesn't know how to make 3 separate arrays<br \/>\nout of 1 by simple assignment.<\/p>\n<p>I could use regular indexing on <tt>c<\/tt> to populate it however. In this case, the right-hand side has length 1 (a cell array with string contents) and the left-hand<br \/>\nside is all of <tt>c<\/tt>. MATLAB <b>can<\/b> do scalar expansion for regular assignment into arrays which is why this works.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">c(:) = {<span style=\"color: #a020f0;\">'ahola'<\/span>}<\/pre>\n<pre style=\"font-style: oblique;\">c = \r\n    'ahola'    'ahola'    'ahola'\r\n<\/pre>\n<p>How else might we populate all the cells in <tt>c<\/tt>? One way is to use the function <tt>deal<\/tt>. I prefer the line above; I think the code without <tt>deal<\/tt> is clearer and it doesn't have the extra function call overhead.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[c{:}] = deal(<span style=\"color: #a020f0;\">'ola'<\/span>)<\/pre>\n<pre style=\"font-style: oblique;\">c = \r\n    'ola'    'ola'    'ola'\r\n<\/pre>\n<p>If I have the values I want to store in the contents of <tt>c<\/tt> in another cell array, let's say <tt>d<\/tt>, I can again use <tt>deal<\/tt> but I don't have to. Compare the following 2 statements.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[c{:}] = deal(d{:})\r\n[c{:}] = d{:}<\/pre>\n<pre style=\"font-style: oblique;\">c = \r\n    'hi'    [17]    [3.1416]\r\nc = \r\n    'hi'    [17]    [3.1416]\r\n<\/pre>\n<p>In the first of these, I expand the cell array on the right to a comma-separated list and pass it through the function <tt>deal<\/tt> which then distributes the outputs into <tt>c<\/tt>. In the second statement, I bypass the function call to <tt>deal<\/tt> with some relatively new (MATLAB 7) syntax.<\/p>\n<h3>Now Explore structs<a name=\"9\"><\/a><\/h3>\n<p>Let's do a similar exercise with structs now.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">s = struct(<span style=\"color: #a020f0;\">'f'<\/span>,a);\r\ns.f<\/pre>\n<pre style=\"font-style: oblique;\">ans =\r\n     1\r\nans =\r\n     8     1     6\r\n     3     5     7\r\n     4     9     2\r\nans =\r\nhello\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #0000ff;\">try<\/span>\r\n    [s.f] = <span style=\"color: #a020f0;\">'bonjour'<\/span>; <span style=\"color: #228b22;\">% error unless s is a scalar<\/span>\r\n<span style=\"color: #0000ff;\">catch<\/span> structE\r\n    disp(structE.message)\r\n<span style=\"color: #0000ff;\">end<\/span><\/pre>\n<pre style=\"font-style: oblique;\">Too many output arguments.\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[s.f] = deal(<span style=\"color: #a020f0;\">'hej'<\/span>);\r\ns.f<\/pre>\n<pre style=\"font-style: oblique;\">ans =\r\nhej\r\nans =\r\nhej\r\nans =\r\nhej\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[s.f] = d{:};\r\ns.f<\/pre>\n<pre style=\"font-style: oblique;\">ans =\r\nhi\r\nans =\r\n    17\r\nans =\r\n    3.1416\r\n<\/pre>\n<p>As you can see, I can bypass the function <tt>deal<\/tt> when assigning to a struct array in analogous way to cell array assignment.<\/p>\n<h3>References<a name=\"14\"><\/a><\/h3>\n<p>Here are some prime references for getting more details on today's topic.<\/p>\n<div>\n<ul>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/deal.html\"><tt>deal<\/tt> Reference Page<\/a><\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/bsxfun.html\"><tt>bsxfun<\/tt> Reference for Singleton Expansion<\/a><\/li>\n<\/ul>\n<\/div>\n<h3>Why Do You Use deal?<a name=\"15\"><\/a><\/h3>\n<p>I'd like to hear from you about situations where you feel using <tt>deal<\/tt> is your only option or you prefer it (perhaps readability?) to the no <tt>deal<\/tt> syntax. Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=123#respond\">here<\/a>.<\/p>\n<p><script>\/\/ <![CDATA[\nfunction grabCode_cb4d1be57fa9433a90bf06e00e8b58ab() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='cb4d1be57fa9433a90bf06e00e8b58ab ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cb4d1be57fa9433a90bf06e00e8b58ab';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Loren Shure';\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\n\n\n\n<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\n\n\n\n\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get<br \/>\nthe MATLAB code<br \/>\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>Published with MATLAB\u00ae 7.5<\/p>\n<\/div>\n<p><!--\ncb4d1be57fa9433a90bf06e00e8b58ab ##### SOURCE BEGIN #####\n%% Deal or No Deal\n% This post continues in the theme from my last post, where people\n% routinely come to me on a topic.  Today I want to distinguish between\n% indexed assignment, where you can take advantage of scalar expansion, and\n% assignment to several output arrays, often arising from a comma-separated\n% list from cell or struct arrays.\n%% First Explore Cell Arrays\n% Let's first explore some aspects of creating and populating the contents\n% of a cell array.  I first initialize 2 arrays, each of length 3.\na = {1 magic(3) 'hello'};\nd = {'hi' 17 pi};\n%%\n% Next I initialize another cell array to be the same as |a|.\nc = a;\n%%\n% Now I attempt to change the contents of all the cells in |c| by assigning\n% a new value that I want to show up in each cell.\ntry\n[c{:}] = 'aloha'; % error unless c is a scalar\ncatch cellE\ndisp(cellE.message)\nend\n%%\n% As you can see, this didn't work.  The reason is that I have 3 entities\n% on the left-hand side, because |c| is a cell array of length 3 and I used\n% the comma-separated list notion.  MATLAB doesn't know how to make 3\n% separate arrays out of 1 by simple assignment.\n%%\n% I could use regular indexing on |c| to populate it however.  In this\n% case, the right-hand side has length 1 (a cell array with string\n% contents) and the left-hand side is all of |c|.  MATLAB *can* do scalar\n% expansion for regular assignment into arrays which is why this works.\nc(:) = {'ahola'}\n%%\n% How else might we populate all the cells in |c|?  One way is to use the\n% function |deal|.  I prefer the line above; I think the code without |deal| is\n% clearer and it doesn't have the extra function call overhead.\n[c{:}] = deal('ola')\n%%\n% If I have the values I want to store in the\n% contents of |c| in another cell array, let's say |d|, I can again use\n% |deal| but I don't have to.  Compare the following 2 statements.\n[c{:}] = deal(d{:})\n[c{:}] = d{:}\n%%\n% In the first of these, I expand the cell array on the right to a\n% comma-separated list and pass it through the function |deal| which then\n% distributes the outputs into |c|.  In the second statement, I bypass the\n% function call to |deal| with some relatively new (MATLAB 7) syntax.\n%% Now Explore structs\n% Let's do a similar exercise with structs now.\ns = struct('f',a);\ns.f\n%%\ntry\n[s.f] = 'bonjour'; % error unless s is a scalar\ncatch structE\ndisp(structE.message)\nend\n%%\n\n[s.f] = deal('hej');\ns.f\n%%\n[s.f] = d{:};\ns.f\n%%\n% As you can see, I can bypass the function |deal| when assigning to a\n% struct array in analogous way to cell array assignment.\n\n%% References\n% Here are some prime references for getting more details on today's topic.\n%\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/deal.html |deal| Reference Page>\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f0-56475.html Comma-Separated Lists>\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f1-86528.html Scalar Expansion>\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/bsxfun.html |bsxfun| Reference for Singleton Expansion>\n%\n%%  Why Do You Use deal?\n% I'd like to hear from you about situations where you feel using |deal| is\n% your only option or you prefer it (perhaps readability?) to the no |deal|\n% syntax.  Let me know\n% <https:\/\/blogs.mathworks.com\/loren\/?p=123#respond here>.\n##### SOURCE END ##### cb4d1be57fa9433a90bf06e00e8b58ab\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nThis post continues in the theme from my last post, where people routinely come to me on a topic. Today I want to distinguish<br \/>\nbetween indexed assignment, where you can take advantage of scalar... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/01\/24\/deal-or-no-deal\/\">read more >><\/a><\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,17,10,4,15,6,8,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/123"}],"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=123"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":1864,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/123\/revisions\/1864"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}