{"id":5043,"date":"2013-08-05T17:33:25","date_gmt":"2013-08-05T16:33:25","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5043"},"modified":"2013-08-06T09:18:19","modified_gmt":"2013-08-06T08:18:19","slug":"generate-a-vector-of-powers-more-quickly-using-cumprod-in-matlab","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5043","title":{"rendered":"Generate a vector of powers more quickly using cumprod in MATLAB"},"content":{"rendered":"<p>While working on someone&#8217;s <a href=\"http:\/\/www.mathworks.co.uk\/products\/matlab\/\">MATLAB<\/a> code today there came a point when it was necessary to generate a vector of powers.\u00a0 For example, [a a^2 a^3&#8230;.a^10000] where a=0.999<\/p>\n<pre>a=0.9999;\r\ny=a.^(1:10000);<\/pre>\n<p>This isn&#8217;t the only way one could form such a vector and I was curious whether or not an alternative method might be faster. On my current machine we have:<\/p>\n<pre>&gt;&gt; tic;y=a.^(1:10000);toc\r\nElapsed time is 0.001526 seconds.\r\n&gt;&gt; tic;y=a.^(1:10000);toc\r\nElapsed time is 0.001529 seconds.\r\n&gt;&gt; tic;y=a.^(1:10000);toc\r\nElapsed time is 0.001716 seconds.<\/pre>\n<p>Let&#8217;s look at the last result in the vector y<\/p>\n<pre>&gt;&gt; y(end)\r\nans =\r\n   0.367861046432970<\/pre>\n<p>So, 0.0015-ish seconds to beat.<\/p>\n<pre>&gt;&gt; tic;y1=cumprod(ones(1,10000)*a);toc\r\nElapsed time is 0.000075 seconds.\r\n&gt;&gt; tic;y1=cumprod(ones(1,10000)*a);toc\r\nElapsed time is 0.000075 seconds.\r\n&gt;&gt; tic;y1=cumprod(ones(1,10000)*a);toc\r\nElapsed time is 0.000075 seconds.<\/pre>\n<p>soundly beaten! More than a factor of 20 in fact. Let&#8217;s check out that last result<\/p>\n<pre>&gt;&gt; y1(end)\r\nans =\r\n   0.367861046432969<\/pre>\n<p>Only a difference in the 15th decimal place&#8211;I&#8217;m happy with that. What I&#8217;m wondering now, however, is will my faster method ever cause me grief?<\/p>\n<p>This is only an academic exercise since this is not exactly a hot spot in the code!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on someone&#8217;s MATLAB code today there came a point when it was necessary to generate a vector of powers.\u00a0 For example, [a a^2 a^3&#8230;.a^10000] where a=0.999 a=0.9999; y=a.^(1:10000); This isn&#8217;t the only way one could form such a vector and I was curious whether or not an alternative method might be faster. On [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53,4,11,7],"tags":[],"class_list":["post-5043","post","type-post","status-publish","format-standard","hentry","category-making-matlab-faster","category-math-software","category-matlab","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1jl","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5043"}],"version-history":[{"count":2,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5043\/revisions"}],"predecessor-version":[{"id":5071,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5043\/revisions\/5071"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}