{"id":5391,"date":"2014-06-20T09:00:33","date_gmt":"2014-06-20T13:00:33","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5391"},"modified":"2014-06-19T17:05:57","modified_gmt":"2014-06-19T21:05:57","slug":"multiple-colon","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/06\/20\/multiple-colon\/","title":{"rendered":"Multiple-Colon"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29854-multiple-colon\"><tt>mcolon<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/29906\">Bruno Luong<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <div>\r\n   <\/div>\r\n\r\nAs some of you may know, I spend an unhealthy amount of time on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/\">MATLAB Answers<\/a> browsing through and answering random questions on MathWorks' products. Hey! I'll argue it's healthier than social media\r\nor watching the World Cup...\r\n<\/br><\/br>\r\nEarlier this week there was a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/134727-how-to-generate-a-single-vector-of-block-consecutive-values-from-2-vectors-of-same-size-without-a-lo\">question<\/a> asking how to most efficiently create a vector from multiple colon subscripts, a common enough task. For example:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #228b22;\">% Starting index<\/span>\r\nstartidx = [1 4 pi]\r\n\r\n<span style=\"color: #228b22;\">% Stride between each start and ending index<\/span>\r\nstride = [1 -1 pi]\r\n\r\n<span style=\"color: #228b22;\">% Ending index<\/span>\r\nendidx = [6 0 pi^2]<\/pre>\r\n<pre style=\"font-style: oblique;\">startidx =\r\n    1.0000    4.0000    3.1416\r\nstride =\r\n    1.0000   -1.0000    3.1416\r\nendidx =\r\n    6.0000         0    9.8696<\/pre>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #228b22;\">% Desired result<\/span>\r\ndisp(v)<\/pre>\r\n<pre style=\"font-style: oblique;\">    1.0000\r\n    2.0000\r\n    3.0000\r\n    4.0000\r\n    5.0000\r\n    6.0000\r\n    4.0000\r\n    3.0000\r\n    2.0000\r\n    1.0000\r\n         0\r\n    3.1416\r\n    6.2832\r\n    9.4248<\/pre>\r\nThis can be accomplished with a simple <tt>for<\/tt>-loop or some indexing tricks which are sufficiently fast for many applications. However, Bruno's <tt>mcolon<\/tt> makes it even faster and more elegant. There are two implementations, one in MATLAB and the other in C++ that can be compiled\r\ninto a <a href=\"\">MEX file<\/a>.\r\n<\/br><\/br>\r\nTo compile the MEX file, you can call the provided <tt>mcolon_install<\/tt>. I really like it when files that need any fancy set up come with their own installation function.\r\n<\/br><\/br>\r\n\r\nAnd now the calling syntax:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">v = mcolon(startidx,stride,endidx).';\r\ndisp(v)<\/pre>\r\n<pre style=\"font-style: oblique;\">    1.0000\r\n    2.0000\r\n    3.0000\r\n    4.0000\r\n    5.0000\r\n    6.0000\r\n    4.0000\r\n    3.0000\r\n    2.0000\r\n    1.0000\r\n         0\r\n    3.1416\r\n    6.2832\r\n    9.4248<\/pre>\r\n<h3>Comments<a name=\"4\"><\/a><\/h3>\r\nDo you have a use-case for creating vectors or arrays from multiple subscripts or strides?\r\n\r\nGive it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5391#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29854-multiple-colon#comments\">comment<\/a> for Bruno.\r\n\r\n<script type=\"text\/javascript\" language=\"JavaScript\">\/\/ <![CDATA[\r\nfunction grabCode_4041be295e624ed3bd7ff699c411dc4d() {\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='4041be295e624ed3bd7ff699c411dc4d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4041be295e624ed3bd7ff699c411dc4d';\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        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\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\\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 href=\"javascript:grabCode_4041be295e624ed3bd7ff699c411dc4d()\"><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code <\/span><\/a><\/p>\r\n<noscript>(requires JavaScript)<\/noscript>\r\n\r\n\r\nPublished with MATLAB\u00ae R2014a\r\n\r\n<\/div>\r\n<!--\r\n4041be295e624ed3bd7ff699c411dc4d ##### SOURCE BEGIN #####\r\n%% Multiple-Colon\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29854-multiple-colon |mcolon|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/29906 Bruno Luong>.\r\n%\r\n\r\n%%\r\n% As some of you may know, I spend an unhealthy amount of time on\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/ MATLAB Answers> browsing\r\n% through and answering random questions on MathWorks' products.  Hey! I'll\r\n% argue it's healthier than social media or watching the World Cup...\r\n%\r\n% Earlier this week there was a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/134727-how-to-generate-a-single-vector-of-block-consecutive-values-from-2-vectors-of-same-size-without-a-lo question> asking\r\n% how to most efficiently create a vector from multiple colon subscripts, a\r\n% common enough task.  For example:\r\n\r\n% Starting index\r\nstartidx = [1 4 pi]\r\n\r\n% Stride between each start and ending index\r\nstride = [1 -1 pi]\r\n\r\n% Ending index\r\nendidx = [6 0 pi^2]\r\n\r\n%%\r\n\r\n% Desired result\r\ndisp(v)\r\n\r\n%%\r\n% This can be accomplished with a simple |for|-loop or some indexing tricks\r\n% which are sufficiently fast for many applications.  However, Bruno's\r\n% |mcolon| makes it even faster and more elegant.  There are two\r\n% implementations, one in MATLAB and the other in C++ that can be compiled\r\n% into a\r\n% < % MEX file>.\r\n%\r\n% To compile the MEX file, you can call the provided |mcolon_install|.  I\r\n% really like it when files that need any fancy set up come with their own\r\n% installation function.\r\n%\r\n% And now the calling syntax:\r\n\r\nv = mcolon(startidx,stride,endidx).';\r\ndisp(v)\r\n\r\n%% Comments\r\n%\r\n% Do you have a use-case for creating vectors or arrays from multiple\r\n% subscripts or strides?\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5391#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29854-multiple-colon#comments % comment> for Bruno.\r\n%\r\n\r\n##### SOURCE END ##### 4041be295e624ed3bd7ff699c411dc4d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Sean's pick this week is mcolon by Bruno Luong.\r\n      \r\n   \r\n   \r\n   \r\n\r\nAs some of you may know, I spend an unhealthy amount of time on MATLAB Answers browsing... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/06\/20\/multiple-colon\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5391"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5391"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5391\/revisions"}],"predecessor-version":[{"id":5396,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5391\/revisions\/5396"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}