{"id":840,"date":"2013-05-15T12:55:36","date_gmt":"2013-05-15T16:55:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=840"},"modified":"2019-11-01T09:17:20","modified_gmt":"2019-11-01T13:17:20","slug":"r2013a-looking-around-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2013\/05\/15\/r2013a-looking-around-in-matlab\/","title":{"rendered":"R2013a &#8211; Looking around in MATLAB"},"content":{"rendered":"\r\n<div class=\"content\"><p>The first MathWorks general product release of the year, <a href=\"https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html\">R2013a<\/a>, shipped a couple of months ago. I've already mentioned it once here in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2013\/03\/12\/matlab-software-testing-tools-old-and-new-r2013a\/\">12-Mar-2013 post<\/a> about the new MATLAB unit test framework.<\/p><p>With each new release, I peruse the release notes for MATLAB to see what things I find particularly interesting. (This helps me remember which product features have actually been released, as opposed to still being in development. My memory needs all the help it can get.)<\/p><p>The first thing to note is the reappearance of the table of contents for navigating in the Help Browser and in the online <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/index.html\">Documentation Center<\/a>. This is a direct result of helpful feedback we received from many of you about the R2012b release.<\/p><p>My favorite \"make-it-go-faster-without-sacrificing-accuracy\" people (the MATLAB Math Team, that is) have been busy again. People with computers based on Intel or AMD chips using the AVX instruction set should see their calls to <tt>fft<\/tt> speed up. Anybody running <tt>permute<\/tt> on 3-D or higher-dimensional arrays should also get a nice boost. I've done a lot of development work related to image and scientific format support, so I know that a fast <tt>permute<\/tt> can be pretty useful when reading image and scientific data. That's because most of these formats store array elements in the file in a different order than MATLAB uses in memory.<\/p><p>In the small-but-nice category, the MATLAB Math Team also simplified a common programming pattern in my own neck of the woods (image processing). Specifically, it's a bit easier to initial an array of 0s or 1s whose type is based on existing array. Here's an example to illustrate:<\/p><pre class=\"codeinput\">clear  <span class=\"comment\">% Let's start with a fresh workspace.<\/span>\r\nrgb = imread(<span class=\"string\">'peppers.png'<\/span>);\r\nimshow(rgb)\r\ntitle(<span class=\"string\">'Obligatory image screenshot'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2013\/r2013a_matlab_01.jpg\" alt=\"\"> <p>Now I want a 100-by-100 matrix of 0s with the same data type as <tt>rgb<\/tt>.<\/p><pre class=\"codeinput\">A = zeros(100,100,<span class=\"string\">'like'<\/span>,rgb); <span class=\"comment\">% Make a 100-by-100 matrix that's \"like\" rgb.<\/span>\r\nwhos\r\n<\/pre><pre class=\"codeoutput\">  Name        Size                Bytes  Class    Attributes\r\n\r\n  A         100x100               10000  uint8              \r\n  rgb       384x512x3            589824  uint8              \r\n\r\n<\/pre><p>My developer friend Tom Bryan really \"likes\" this (ahem) because it enables much easier solutions to some common programming tasks for users of Fixed-Point Designer.<\/p><p>I have occasionally done a little web scripting in MATLAB, so it's nice to see <tt>urlread<\/tt> and <tt>urlwrite<\/tt> get a little love. These functions can now handle basic authentication via the <tt>'Authentication'<\/tt>, <tt>'Username'<\/tt>, and <tt>'Password'<\/tt> parameters.<\/p><p>Do you use a Mac? You can now write MPEG-4 H.264 files using <tt>VideoWriter<\/tt> (requires Mac OS 10.7 or later).<\/p><p>A couple of handy new string functions have appeared, <tt>strsplit<\/tt> and <tt>strjoin<\/tt>. Based on how often users have submitted their own versions to the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/\">MATLAB Central File Exchange<\/a>, I'm sure these will be popular.<\/p><pre class=\"codeinput\">out = strsplit(pwd,<span class=\"string\">'\\'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nout = \r\n\r\n    'B:'    'published'    '2013'\r\n\r\n<\/pre><p>You can now do extrapolation with both scattered and gridded interpolation. For extrapolation with scattered interpolation, use the new <tt>scatteredInterpolant<\/tt>. Here's an example I lifted from the doc.<\/p><p>Query the interpolant at a single point outside the convex hull using nearest neighbor extrapolation.<\/p><p>Define a matrix of 200 random points.<\/p><pre class=\"codeinput\">P = -2.5 + 5*gallery(<span class=\"string\">'uniformdata'<\/span>,[200 2],0);\r\n<\/pre><p>Sample an exponential function. These are the sample values for the interpolant.<\/p><pre class=\"codeinput\">x = P(:,1);\r\ny = P(:,2);\r\nv = x.*exp(-x.^2-y.^2);\r\n<\/pre><p>Create the interpolant, specifying linear interpolation and nearest neighbor extrapolation.<\/p><pre class=\"codeinput\">F = scatteredInterpolant(P,v,<span class=\"string\">'linear'<\/span>,<span class=\"string\">'nearest'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nF = \r\n\r\n  scatteredInterpolant with properties:\r\n\r\n                 Points: [200x2 double]\r\n                 Values: [200x1 double]\r\n                 Method: 'linear'\r\n    ExtrapolationMethod: 'nearest'\r\n\r\n<\/pre><p>Evaluate the interpolant outside the convex hull.<\/p><pre class=\"codeinput\">vq = F(3.0,-1.5)\r\n<\/pre><pre class=\"codeoutput\">\r\nvq =\r\n\r\n    0.0031\r\n\r\n<\/pre><p>Disable extrapolation and evaluate F at the same point.<\/p><pre class=\"codeinput\">F.ExtrapolationMethod = <span class=\"string\">'none'<\/span>;\r\nvq = F(3.0,-1.5)\r\n<\/pre><pre class=\"codeoutput\">\r\nvq =\r\n\r\n   NaN\r\n\r\n<\/pre><p>I encourage you to wander over to the R2013a release notes for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html\">MATLAB<\/a> or any other product that you use and see what's new that might be helpful to you.<\/p><p>There are also lots of new things in the image processing and computer vision worlds, of course. I'll look at those next time.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_e908749212e9435394509bf494f27583() {\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='e908749212e9435394509bf494f27583 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e908749212e9435394509bf494f27583';\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        copyright = 'Copyright 2013 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_e908749212e9435394509bf494f27583()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p><\/div><!--\r\ne908749212e9435394509bf494f27583 ##### SOURCE BEGIN #####\r\n%%\r\n% The first MathWorks general product release of the year,\r\n% <https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html\r\n% R2013a>, shipped a couple of months ago. I've already mentioned it once\r\n% here in my <https:\/\/blogs.mathworks.com\/steve\/2013\/03\/12\/matlab-software-testing-tools-old-and-new-r2013a\/\r\n% 12-Mar-2013 post> about the new MATLAB unit test framework.\r\n%\r\n% With each new release, I peruse the release notes for MATLAB to see what\r\n% things I find particularly interesting. (This helps me remember which\r\n% product features have actually been released, as opposed to still being\r\n% in development. My memory needs all the help it can get.)\r\n%\r\n% The first thing to note is the reappearance of the table of contents for\r\n% navigating in the Help Browser and in the online \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/index.html Documentation Center>.\r\n% This is a direct result of helpful feedback we received from many of you\r\n% about the R2012b release.\r\n%\r\n% My favorite \"make-it-go-faster-without-sacrificing-accuracy\" people (the\r\n% MATLAB Math Team, that is) have been busy again. People with computers\r\n% based on Intel or AMD chips using the AVX instruction set should see\r\n% their calls to |fft| speed up. Anybody running |permute| on 3-D or\r\n% higher-dimensional arrays should also get a nice boost. I've done a lot\r\n% of development work related to image and scientific format support, so I\r\n% know that a fast |permute| can be pretty useful when reading image and\r\n% scientific data. That's because most of these formats store array\r\n% elements in the file in a different order than MATLAB uses in memory.\r\n%\r\n% In the small-but-nice category, the MATLAB Math Team also simplified a\r\n% common programming pattern in my own neck of the woods (image\r\n% processing). Specifically, it's a bit easier to initial an array of 0s or\r\n% 1s whose type is based on existing array. Here's an example to\r\n% illustrate:\r\n\r\nclear  % Let's start with a fresh workspace.\r\nrgb = imread('peppers.png');\r\nimshow(rgb)\r\ntitle('Obligatory image screenshot')\r\n\r\n%%\r\n% Now I want a 100-by-100 matrix of 0s with the same data type as |rgb|.\r\n\r\nA = zeros(100,100,'like',rgb); % Make a 100-by-100 matrix that's \"like\" rgb.\r\nwhos\r\n\r\n%%\r\n% My developer friend Tom Bryan really \"likes\" this (ahem) because it\r\n% enables much easier solutions to some common programming tasks for users\r\n% of Fixed-Point Designer.\r\n\r\n%%\r\n% I have occasionally done a little web scripting in MATLAB, so it's nice\r\n% to see |urlread| and |urlwrite| get a little love. These functions can\r\n% now handle basic authentication via the |'Authentication'|, |'Username'|,\r\n% and |'Password'| parameters.\r\n%\r\n% Do you use a Mac? You can now write MPEG-4 H.264 files using\r\n% |VideoWriter| (requires Mac OS 10.7 or later).\r\n%\r\n% A couple of handy new string functions have appeared, |strsplit| and\r\n% |strjoin|. Based on how often users have submitted their own versions to\r\n% the <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/ \r\n% MATLAB Central File Exchange>, I'm sure these will be popular.\r\n\r\nout = strsplit(pwd,'\\')\r\n\r\n%%\r\n% You can now do extrapolation with both scattered and gridded\r\n% interpolation. For extrapolation with scattered interpolation, use the\r\n% new |scatteredInterpolant|. Here's an example I lifted from the doc.\r\n%\r\n% Query the interpolant at a single point outside the convex hull using\r\n% nearest neighbor extrapolation.\r\n%\r\n% Define a matrix of 200 random points.\r\nP = -2.5 + 5*gallery('uniformdata',[200 2],0);\r\n\r\n%% \r\n% Sample an exponential function. These are the sample values for the\r\n% interpolant.\r\nx = P(:,1);\r\ny = P(:,2);\r\nv = x.*exp(-x.^2-y.^2);\r\n\r\n%%\r\n% Create the interpolant, specifying linear interpolation and nearest\r\n% neighbor extrapolation.\r\nF = scatteredInterpolant(P,v,'linear','nearest')\r\n\r\n%%\r\n% Evaluate the interpolant outside the convex hull.\r\nvq = F(3.0,-1.5)\r\n\r\n%%\r\n% Disable extrapolation and evaluate F at the same point.\r\nF.ExtrapolationMethod = 'none';\r\nvq = F(3.0,-1.5)\r\n\r\n%%\r\n% I encourage you to wander over to the R2013a release notes for \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html MATLAB> or any\r\n% other product that you use and see what's new that might be helpful to\r\n% you.\r\n%\r\n% There are also lots of new things in the image processing and computer vision\r\n% worlds, of course. I'll look at those next time.\r\n\r\n##### SOURCE END ##### e908749212e9435394509bf494f27583\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2013\/r2013a_matlab_01.jpg\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nThe first MathWorks general product release of the year, R2013a, shipped a couple of months ago. I've already mentioned it once here in my 12-Mar-2013 post about the new MATLAB unit test... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2013\/05\/15\/r2013a-looking-around-in-matlab\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[302,508,1009,76,36,1007,1005,1003,52,1001,731,719,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/840"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=840"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/840\/revisions"}],"predecessor-version":[{"id":844,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/840\/revisions\/844"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}