{"id":5140,"date":"2013-09-25T11:13:35","date_gmt":"2013-09-25T10:13:35","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5140"},"modified":"2014-02-26T10:24:34","modified_gmt":"2014-02-26T09:24:34","slug":"lamenting-the-lack-of-multiple-assignment-in-matlab","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5140","title":{"rendered":"Lamenting the lack of multiple assignment in MATLAB"},"content":{"rendered":"<p>I support scientific applications at <a href=\"http:\/\/www.manchester.ac.uk\/\">The University of Manchester<\/a> (see my <a href=\"http:\/\/www.linkedin.com\/profile\/view?id=45662859\">LinkedIn profile<\/a> if you&#8217;re interested in the details) and part of my job involves working on code written by researchers in a variety of languages. \u00a0When I say &#8216;variety&#8217; I really mean it &#8211; MATLAB, Mathematica, Python, C, Fortran, Julia, Maple, Visual Basic and PowerShell are some languages I&#8217;ve worked with this month for instance.<\/p>\n<p>Having to juggle the semantics of so many languages in my head sometimes leads to momentary confusion when working on someone&#8217;s program. \u00a0For example, I&#8217;ve been doing a lot of Python work recently but this morning I was hacking away on someone&#8217;s MATLAB code. \u00a0Buried deep within the program, it would have been very sensible to be able to do the equivalent of this:<\/p>\n<pre>a=rand(3,3)\r\n\r\na =\r\n    0.8147    0.9134    0.2785\r\n    0.9058    0.6324    0.5469\r\n    0.1270    0.0975    0.9575\r\n\r\n&gt;&gt; [x,y,z]=a(:,1)\r\n\r\nIndexing cannot yield multiple results.<\/pre>\n<p>That is, I want to be able to take the first column of the matrix a and broadcast it out to the variables x,y and z. The code I&#8217;m working on uses MUCH bigger matrices and this kind of assignment is occasionally useful since the variable names x,y,z have slightly more meaning than a(1,3), a(2,3), a(3,3).<\/p>\n<p>The only concise way I&#8217;ve been able to do something like this using native MATLAB commands is to first convert to a cell. In MATLAB 2013a for instance:<\/p>\n<pre>&gt;&gt; temp=num2cell(a(:,1));\r\n&gt;&gt; [x y z] = temp{:}\r\n\r\nx =\r\n    0.8147\r\n\r\ny =\r\n    0.9058\r\n\r\nz =\r\n    0.1270<\/pre>\n<p>This works but I think it looks ugly and introduces conversion overheads. The problem I had for a short time is that I subconsciously expected multiple assignment to &#8216;Just Work&#8217; in MATLAB since the concept makes sense in several other languages I use regularly.<\/p>\n<p><a href=\"http:\/\/www.python.org\/\">Python<\/a>:<\/p>\n<pre>from pylab import rand\r\na=rand(3,3)\r\n[a,b,c]=a[:,0]<\/pre>\n<p><a href=\"http:\/\/www.wolfram.com\/mathematica\/\">Mathematica<\/a>:<\/p>\n<pre>a = RandomReal[1, {3, 3}]\r\n{x,y,z}=a[[All,1]]<\/pre>\n<p><a href=\"http:\/\/julialang.org\/\">Julia<\/a>:<\/p>\n<pre>a=rand(3,3);\r\n(x,y,z)=a[:,1]<\/pre>\n<p>I&#8217;ll admit that I don&#8217;t often need this construct in MATLAB but it would definitely be occasionally useful. I wonder what other opinions there are out there? Do you think multiple assignment is useful (in any language)?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I support scientific applications at The University of Manchester (see my LinkedIn profile if you&#8217;re interested in the details) and part of my job involves working on code written by researchers in a variety of languages. \u00a0When I say &#8216;variety&#8217; I really mean it &#8211; MATLAB, Mathematica, Python, C, Fortran, Julia, Maple, Visual Basic and [&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":[59,8,11,7,31],"tags":[],"class_list":["post-5140","post","type-post","status-publish","format-standard","hentry","category-julia","category-mathematica","category-matlab","category-programming","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1kU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5140","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=5140"}],"version-history":[{"count":5,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5140\/revisions"}],"predecessor-version":[{"id":5378,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5140\/revisions\/5378"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}