{"id":1427,"date":"2016-04-15T06:04:23","date_gmt":"2016-04-15T11:04:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1427"},"modified":"2016-08-04T09:21:54","modified_gmt":"2016-08-04T14:21:54","slug":"moving-along","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2016\/04\/15\/moving-along\/","title":{"rendered":"Moving Along"},"content":{"rendered":"<div class=\"content\"><!--introduction-->Ever wish you could easily perform some statistical calculations with a sliding window, but find convolution and filtering a bit daunting for this application? Then a set of new functions in <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html#R2016a\">Release R2016a<\/a> might just be for you!\r\n\r\n<!--\/introduction-->\r\n<h3>Contents<\/h3>\r\n<div>\r\n<ul>\r\n \t<li><a href=\"#a4465cf8-552d-430e-bb74-f171632e6dc2\">New Collection of Functions for Moving Operations<\/a><\/li>\r\n \t<li><a href=\"#2f70dac1-dbbf-4d3a-ae2c-a036e661628a\">Find the sliding maxima<\/a><\/li>\r\n \t<li><a href=\"#7c3ffdc2-0c62-419f-9f36-d6563d8fe794\">So much easier now!<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h4>New Collection of Functions for Moving Operations<a name=\"a4465cf8-552d-430e-bb74-f171632e6dc2\"><\/a><\/h4>\r\nThe new collection of functions for moving (or sliding) operations are:\r\n<div>\r\n<ul>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmean.html\">movmean<\/a><\/tt><\/li>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmedian.html\">movmedian<\/a><\/tt><\/li>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movstd.html\">movstd<\/a><\/tt><\/li>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movsum.html\">movsum<\/a><\/tt><\/li>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movvar.html\">movvar<\/a><\/tt><\/li>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmean.html\">movmean<\/a><\/tt><\/li>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmax.html\">movmax<\/a><\/tt><\/li>\r\n \t<li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmin.html\">movmin<\/a><\/tt><\/li>\r\n<\/ul>\r\n<\/div>\r\nThese functions complement the <tt>cum*<\/tt> functions, some of which have been in MATLAB for a long time, e.g., <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cumsum.html\">cumsum<\/a><\/tt>, and some of which are relatively new, e.g., * <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cummax.html\">cummax<\/a><\/tt>.\r\n<h4>Find the sliding maxima<a name=\"2f70dac1-dbbf-4d3a-ae2c-a036e661628a\"><\/a><\/h4>\r\nOf course you could always find a local maximum in MATLAB by using a <tt>for<\/tt>-loop and working your way down the data, but paying careful attention to the ends. Or, with judicious use of indexing and reshaping, you could get the answer in a more vectorized fashion. Now you can just get it!\r\n<pre class=\"codeinput\">ndata = 100;\r\ndata = 0.5*rand(ndata,1)+0.5;\r\nnpts = 5;\r\ndatamvmax = movmax(data, npts);\r\nplot(data,<span class=\"string\">'g*'<\/span>)\r\naxis([0 100 0.4 1.1])\r\nhold <span class=\"string\">on<\/span>\r\nplot(datamvmax,<span class=\"string\">'b+'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/loren\/files\/movingAlong_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h4>So much easier now!<a name=\"7c3ffdc2-0c62-419f-9f36-d6563d8fe794\"><\/a><\/h4>\r\nComputing moving results is much easier now. And with several options that will also make your computing life better - e.g., choice on handling <tt>NaN<\/tt> values, choice of direction, allowing a window that may not be symmetric with respect to lagging and leading values, and how to handle the endpoints. Will these new functions simplify your MATLAB code? \r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_20aa28ad89a1466393cf1bb9a8833c19() {\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='20aa28ad89a1466393cf1bb9a8833c19 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 20aa28ad89a1466393cf1bb9a8833c19';\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 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<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>\r\n\r\n\r\n\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae R2016a<\/p>\r\n\r\n<\/div>\r\n<!--\r\n20aa28ad89a1466393cf1bb9a8833c19 ##### SOURCE BEGIN #####\r\n%% Moving Along\r\n% Ever wish you could easily perform some statistical calculations with a\r\n% sliding window, but find convolution and filtering a bit daunting for\r\n% this application? Then a set of new functions in\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html#R2016a Release % R2016a> might just be for you!\r\n%% New Collection of Functions for Moving Operations\r\n% The new collection of functions for moving (or sliding) operations are:\r\n%\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmean.html movmean>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmedian.html movmedian>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/modstd.html movstd>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movsum.html movsum>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movvar.html movvar>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmean.html movmean>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmax.html movmax>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movmin.html movmin>|\r\n%\r\n% These functions complement the |cum*| functions, some of which have been\r\n% in MATLAB for a long time, e.g.,\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/cumsum.html cumsum>|, and some\r\n% of which are relatively new, e.g., *\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/cummax.html cummax>|.\r\n%% Find the sliding maxima\r\n% Of course you could always find a local maximum in MATLAB by using a\r\n% |for|-loop and working your way down the data, but paying careful\r\n% attention to the ends.  Or, with judicious use of indexing and reshaping,\r\n% you could get the answer in a more vectorized fashion.  Now you can just\r\n% get it!\r\n%\r\n%%\r\nndata = 100;\r\ndata = 0.5*rand(ndata,1)+0.5;\r\nnpts = 5;\r\ndatamvmax = movmax(data, npts);\r\nplot(data,'g*')\r\naxis([0 100 0.4 1.1])\r\nhold on\r\nplot(datamvmax,'b+')\r\nhold off\r\n%% So much easier now!\r\n% Computing moving results is much easier now.  And with several options\r\n% that will also make your computing life better - e.g., choice on handling\r\n% |NaN| values, choice of direction, allowing a window that may not be\r\n% symmetric with respect to lagging and leading values, and how to handle\r\n% the endpoints.  Will these new functions simplify your MATLAB code?  Let\r\n% us know <https:\/\/blogs.mathworks.com\/loren\/?p=1422#respond here>.\r\n##### SOURCE END ##### 20aa28ad89a1466393cf1bb9a8833c19\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/loren\/files\/movingAlong_01.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction-->Ever wish you could easily perform some statistical calculations with a sliding window, but find convolution and filtering a bit daunting for this application? Then a set of new functions in <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html#R2016a\">Release R2016a<\/a> might just be for you!\r\n\r\n<!--\/introduction-->... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/04\/15\/moving-along\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1427"}],"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=1427"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1427\/revisions"}],"predecessor-version":[{"id":1976,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1427\/revisions\/1976"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}