{"id":789,"date":"2009-03-13T14:09:03","date_gmt":"2009-03-13T13:09:03","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=789"},"modified":"2009-03-13T14:09:03","modified_gmt":"2009-03-13T13:09:03","slug":"fun-with-linspace-and-the-colon-operator-in-matlab","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=789","title":{"rendered":"Fun with linspace and the colon operator in MATLAB"},"content":{"rendered":"<p>Say you want a vector that starts at 0 and goes to 1 in steps of 0.1<br \/>\nThere are two ways you might do this in MATLAB<\/p>\n<p><strong>x=0:0.1:1<br \/>\ny=linspace(0,1,11)<\/strong><\/p>\n<p>If you display either x or y then you will get<\/p>\n<p><strong>[0\u00a0\u00a0\u00a0 0.1000\u00a0\u00a0\u00a0 0.2000\u00a0\u00a0\u00a0 0.3000\u00a0\u00a0\u00a0 0.4000\u00a0\u00a0\u00a0 0.5000\u00a0\u00a0\u00a0 0.6000\u00a0\u00a0\u00a0 0.7000\u00a0\u00a0\u00a0 0.8000\u00a0\u00a0\u00a0 0.9000\u00a0\u00a0\u00a0 1.0000]<\/strong><\/p>\n<p>So it looks like we are in clover.\u00a0 However if we do<\/p>\n<p><strong>x==y<\/strong><\/p>\n<p>we get<\/p>\n<p><strong>[1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1]<\/strong><\/p>\n<p>Which shows that the vectors x and y are not EXACTLY equal for all values (1 stands for true and 0 stands for false so one value is different).\u00a0 We can see that the difference is extremely small by doing<\/p>\n<p><strong>x-y<\/strong><br \/>\n<code><br \/>\n<strong>1.0e-16 *<br \/>\n0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0 0.5551\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0<\/strong><br \/>\n<\/code><\/p>\n<p>This tiny difference is almost certainly caused by the fact that the colon operator and linspace use slightly different methods to calculate the vectors as pointed out in the <a href=\"http:\/\/www.mathworks.co.uk\/matlabcentral\/newsreader\/view_thread\/244425\">newsgroup post<\/a> where I discovered this little piece of trivia.\u00a0 .\u00a0 If floating point arithmetic was exact then it wouldn&#8217;t matter what algorithm you used to calculate a vector like this &#8211; the result would always be the same as you would expect.<\/p>\n<p>However, floating point arithmetic isn&#8217;t exact and so things like the order of operations matters.\u00a0 This sort of thing has been <a href=\"http:\/\/en.wikipedia.org\/wiki\/Floating_point\">discussed<\/a> many times by <a href=\"http:\/\/www.johndcook.com\/blog\/2008\/09\/28\/theoretical-explanation-for-numerical-results\/\">people<\/a> considerably <a href=\"http:\/\/docs.sun.com\/source\/806-3568\/ncg_goldberg.html\">more erudite<\/a> than me so I refer you <a href=\"http:\/\/docs.python.org\/tutorial\/floatingpoint.html\">to them<\/a> for details.<\/p>\n<p>What I was curious about was <strong>&#8216;How,exactly, does the colon operator calculate it&#8217;s values and how does this differ from linspace?&#8217;<\/strong> Oh yes, I am a sucker for mathematical trivia.<\/p>\n<p>If I didn&#8217;t have access to either of these methods in MATLAB then I would calculate our vector like this<\/p>\n<p><strong>n=0:10<br \/>\na=n*0.1<\/strong><\/p>\n<p>But it turns out that this isn&#8217;t equal to either x or y so this isn&#8217;t how linspace or the colon operator works:<\/p>\n<p><strong>a==x<\/strong> (compare to colon operator)<\/p>\n<p><strong>1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1<\/strong><\/p>\n<p><strong>a==y<\/strong> (compare to linspace)<\/p>\n<p><strong>1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1<\/strong><\/p>\n<p>Another way for calculating this vector that springs to mind is to start with 0 and repeatedly add 0.1 until you reach 1.\u00a0 I normally would never do it like this due to the possibility of accumulating rounding errors on each addition but let&#8217;s take a look.<br \/>\n<code><strong><br \/>\nb=[zeros(1,11)];<br \/>\nfor i=2:11<br \/>\nb(i)=b(i-1)+0.1;<br \/>\nend<\/strong><\/code><\/p>\n<p>As I&#8217;d expect, this isn&#8217;t how MATLAB does it either.<\/p>\n<p><strong>b==x<\/strong> (compare to colon operator)<\/p>\n<p><strong>1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 0<\/strong><\/p>\n<p><strong>b==y<\/strong> (compare to linspace)<\/p>\n<p><strong>1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0 0<\/strong><\/p>\n<p>My methods don&#8217;t agree with each other either (do<strong> a==b<\/strong> to see) but I was expecting that.<\/p>\n<p>So, I am non the wiser.\u00a0 How <strong>does<\/strong> linspace and the colon operator work? Thoughts and comments would be welcomed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say you want a vector that starts at 0 and goes to 1 in steps of 0.1 There are two ways you might do this in MATLAB x=0:0.1:1 y=linspace(0,1,11) If you display either x or y then you will get [0\u00a0\u00a0\u00a0 0.1000\u00a0\u00a0\u00a0 0.2000\u00a0\u00a0\u00a0 0.3000\u00a0\u00a0\u00a0 0.4000\u00a0\u00a0\u00a0 0.5000\u00a0\u00a0\u00a0 0.6000\u00a0\u00a0\u00a0 0.7000\u00a0\u00a0\u00a0 0.8000\u00a0\u00a0\u00a0 0.9000\u00a0\u00a0\u00a0 1.0000] So it looks like [&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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,11,7],"tags":[],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-math-software","category-matlab","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-cJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/789","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=789"}],"version-history":[{"count":4,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/789\/revisions"}],"predecessor-version":[{"id":793,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/789\/revisions\/793"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}