{"id":75,"date":"2007-01-31T08:51:37","date_gmt":"2007-01-31T13:51:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=75"},"modified":"2007-04-05T06:00:56","modified_gmt":"2007-04-05T11:00:56","slug":"multiple-outputs","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/01\/31\/multiple-outputs\/","title":{"rendered":"Multiple Outputs"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I just had an interesting exchange with a customer.  He was trying to have an anonymous function return multiple outputs.<\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Inherent Multiple Outputs<\/a><\/li>\r\n         <li><a href=\"#4\">Assembling Multiple Outputs<\/a><\/li>\r\n         <li><a href=\"#9\">Thoughts?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Inherent Multiple Outputs<a name=\"1\"><\/a><\/h3>\r\n   <p>You can inherently get multiple outputs from an anonymous function if the function being called returns more than a single\r\n      output.  Here's an example.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fdoubleEig = @(x) eig(2*x)<\/pre><pre style=\"font-style:oblique\">fdoubleEig = \r\n    @(x) eig(2*x)\r\n<\/pre><p>Let's ask for a single output first.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">eOnly = fdoubleEig(magic(3))<\/pre><pre style=\"font-style:oblique\">eOnly =\r\n   30.0000\r\n    9.7980\r\n   -9.7980\r\n<\/pre><p>and now let's get 2 outputs, the eigenvalues and the eigenvectors.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[e,v] = fdoubleEig(magic(3))<\/pre><pre style=\"font-style:oblique\">e =\r\n   -0.5774   -0.8131   -0.3416\r\n   -0.5774    0.4714   -0.4714\r\n   -0.5774    0.3416    0.8131\r\nv =\r\n   30.0000         0         0\r\n         0    9.7980         0\r\n         0         0   -9.7980\r\n<\/pre><h3>Assembling Multiple Outputs<a name=\"4\"><\/a><\/h3>\r\n   <p>If you want to return multiple outputs and there isn't an existing function to do so, you can assemble the outputs using the\r\n      function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/deal.html\"><tt>deal<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fmeanVar = @(x) deal(mean(x), var(x))<\/pre><pre style=\"font-style:oblique\">fmeanVar = \r\n    @(x) deal(mean(x), var(x))\r\n<\/pre><p>Let's try to get a single output.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    mOnly = fmeanVar(magic(3));\r\n<span style=\"color: #0000FF\">catch<\/span>\r\n    disp(<span style=\"color: #A020F0\">'deal requires the exact number of outputs.'<\/span>)\r\n    lerr = lasterror;\r\n    disp(lerr.message)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">deal requires the exact number of outputs.\r\nError using ==&gt; deal\r\nThe number of outputs should match the number of inputs.\r\n<\/pre><p>This doesn't work because <tt>deal<\/tt> is picky about the number of outputs it expects to see.\r\n   <\/p>\r\n   <p>Now let's try with 2 outputs.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[m,v] = fmeanVar(magic(3))<\/pre><pre style=\"font-style:oblique\">m =\r\n     5     5     5\r\nv =\r\n     7    16     7\r\n<\/pre><p>The <tt>deal<\/tt> function now sees two left-hand sides and works.\r\n   <\/p>\r\n   <h3>Thoughts?<a name=\"9\"><\/a><\/h3>\r\n   <p>I suspect you now understand this (for those who didn't already), but I suspect I can hear protests about how MATLAB should\r\n      be smarter about this.  If you have some concrete thoughts, I'd love to hear them <a href=\"?p=75#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_4781d9c68e3f457cb65550136679a2c2() {\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='4781d9c68e3f457cb65550136679a2c2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4781d9c68e3f457cb65550136679a2c2';\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_4781d9c68e3f457cb65550136679a2c2()\"><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.3<br><\/p>\r\n<\/div>\r\n<!--\r\n4781d9c68e3f457cb65550136679a2c2 ##### SOURCE BEGIN #####\r\n%% Multiple Outputs\r\n% I just had an interesting exchange with a customer.  He was trying to\r\n% have an anonymous function return multiple outputs.\r\n%% Inherent Multiple Outputs\r\n% You can inherently get multiple outputs from an anonymous function if the\r\n% function being called returns more than a single output.  Here's an\r\n% example.\r\nfdoubleEig = @(x) eig(2*x)\r\n%%\r\n% Let's ask for a single output first.\r\neOnly = fdoubleEig(magic(3))\r\n%%\r\n% and now let's get 2 outputs, the eigenvalues and the eigenvectors.\r\n[e,v] = fdoubleEig(magic(3))\r\n%% Assembling Multiple Outputs\r\n% If you want to return multiple outputs and there isn't an existing\r\n% function to do so, you can assemble the outputs using the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/deal.html |deal|>.\r\nfmeanVar = @(x) deal(mean(x), var(x)) \r\n%%\r\n% Let's try to get a single output.\r\ntry\r\n    mOnly = fmeanVar(magic(3));\r\ncatch\r\n    disp('deal requires the exact number of outputs.')\r\n    lerr = lasterror;\r\n    disp(lerr.message)\r\nend\r\n%%\r\n% This doesn't work because |deal| is picky about the number of outputs it\r\n% expects to see.\r\n%%\r\n% Now let's try with 2 outputs.\r\n[m,v] = fmeanVar(magic(3))\r\n%%\r\n% The |deal| function now sees two left-hand sides and works.\r\n%% Thoughts?\r\n% I suspect you now understand this (for those who didn't already), but I\r\n% suspect I can hear protests about how MATLAB should be smarter about\r\n% this.  If you have some concrete thoughts, I'd love to hear them\r\n% <?p=75#respond here>.  \r\n##### SOURCE END ##### 4781d9c68e3f457cb65550136679a2c2\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I just had an interesting exchange with a customer.  He was trying to have an anonymous function return multiple outputs.\r\n   \r\n   Contents\r\n   \r\n      \r\n    ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/01\/31\/multiple-outputs\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/75"}],"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=75"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}