{"id":271,"date":"2011-04-11T13:27:30","date_gmt":"2011-04-11T13:27:30","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/04\/11\/size-does-matter-sometimes\/"},"modified":"2011-04-11T09:29:55","modified_gmt":"2011-04-11T09:29:55","slug":"size-does-matter-sometimes","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/04\/11\/size-does-matter-sometimes\/","title":{"rendered":"size Does Matter Sometimes"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Recently at work I was on an email thread where people were discussing the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/size.html\"><tt>size<\/tt><\/a> and how to use it reliably in the context of arrays that might be two dimensional but could have higher dimensions as well.\r\n          The original solutions resorted to calling <tt>size<\/tt> multiple times.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>There is a better way, however, and I pointed it out.  The idea is to use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/matlab_prog\/bresuxt-1.html#br67dkp-1\"><tt>~<\/tt><\/a> to capture all the remaining dimensions after the first two.  Like so:\r\n   <\/p><pre>  [rows, cols, ~] = size(A);<\/pre><p><a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/03\/22\/too-much-information-about-the-size-function\/\">Steve<\/a> has a much more in depth expos&eacute; of the problem and solution, and a <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/03\/29\/even-more-information-about-the-size-function\/\">follow-on post<\/a>.\r\n   <\/pre><p><\/p>\r\n   <p>Instead of using <tt>~<\/tt>, you could choose to use a junk variable and ignore it, but I (and Steve) prefer to use this relatively new feature and not\r\n      clutter the code or workspace with something that is a distraction.\r\n   <\/p>\r\n   <p>Have you used <tt>~<\/tt> to ignore outputs from a function, or in a function definition to ignore an input?  Let me know some details <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=271#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_640d7ac063124334be86e12a270a6845() {\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='640d7ac063124334be86e12a270a6845 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 640d7ac063124334be86e12a270a6845';\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 2011 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_640d7ac063124334be86e12a270a6845()\"><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\n640d7ac063124334be86e12a270a6845 ##### SOURCE BEGIN #####\r\n%% size Does Matter Sometimes\r\n% Recently at work I was on an email thread where people were discussing\r\n% the function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/size.html\r\n% |size|> and how to use it reliably in the context of arrays that might be\r\n% two dimensional but could have higher dimensions as well.  The original\r\n% solutions resorted to calling |size| multiple times.\r\n%%\r\n% There is a better way, however, and I pointed it out.  The idea is to use\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/matlab_prog\/bresuxt-1.html#br67dkp-1\r\n% |~|> to capture all the remaining dimensions after the first two.  Like\r\n% so:\r\n%\r\n%    [rows, cols, ~] = size(A);\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/03\/22\/too-much-information-about-the-size-function\/\r\n% Steve> has a much more in depth expos\u00c3\u00a9 of the problem and solution.\r\n%%\r\n% Instead of using |~|, you could choose to use a junk variable and ignore\r\n% it, but I (and Steve) prefer to use this relatively new feature and not\r\n% clutter the code or workspace with something that is a distraction.\r\n%%\r\n% Have you used |~| to ignore outputs from a function, or in a function\r\n% definition to ignore an input?  Let me know some details\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=271#respond here>.\r\n##### SOURCE END ##### 640d7ac063124334be86e12a270a6845\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Recently at work I was on an email thread where people were discussing the function size and how to use it reliably in the context of arrays that might be two dimensional but could have... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/04\/11\/size-does-matter-sometimes\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,15,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/271"}],"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=271"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}