{"id":41,"date":"2016-07-24T10:11:39","date_gmt":"2016-07-24T18:11:39","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=41"},"modified":"2018-08-20T19:50:44","modified_gmt":"2018-08-21T03:50:44","slug":"matlab-fundamentals-vectorization","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2016\/07\/24\/matlab-fundamentals-vectorization\/","title":{"rendered":"MATLAB Fundamentals: Vectorization"},"content":{"rendered":"<p>A coworker whose background is in embedded systems (with a C background and no MATLAB at all), after hearing my rants that people are coding MATLAB like C using way more for-loops than necessary,\u00a0asked me if he has two vectors,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\" data-enlighter-linenumbers=\"false\">a = 0:32767;\r\nb = 0:32767;<\/pre>\n<p>and he want\u00a0<span style=\"text-decoration: underline;\">all\u00a0combinations<\/span> of the elements in <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-0e55b0b3943237ccfc96979505679274_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"9\" style=\"vertical-align: 0px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-ad69adf868bc701e561aa555db995f1f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#98;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"8\" style=\"vertical-align: 0px;\"\/> so that for each index pair <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-f59b4e05c77cfa051fe518e32484839c_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#105;&#44;&#32;&#106;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"34\" style=\"vertical-align: -5px;\"\/>, he will get<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 44px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-43568ef19d3f801a0929142be0f0f504_l3.png\" height=\"44\" width=\"111\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91;&#32;&#49;&#54;&#55;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#102;&#114;&#97;&#99;&#123;&#97;&#95;&#106;&#43;&#52;&#50;&#125;&#123;&#98;&#95;&#106;&#43;&#49;&#55;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p>There are <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-979b16392bf2dd7768914c70fbc282f0_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#51;&#50;&#55;&#54;&#56;&#94;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"52\" style=\"vertical-align: 0px;\"\/> combinations out there. At first, I showed him the typical method shown in the MATLAB&#8217;s introduction materials:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">% Should have used ndgrid() for a more natural (column first) layout\r\n[B, A] = meshgrid(a, b);\r\n\r\nC = 167*(A+42).\/(B+17)<\/pre>\n<p>Then he asked, &#8216;This way I have to store the matrices <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-816b613a4f79d4bf9cb51396a9654120_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#65;\" title=\"Rendered by QuickLaTeX.com\" height=\"13\" width=\"13\" style=\"vertical-align: 0px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-c74288aabc0e2ca280d25d92bf1a1ec2_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#66;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"14\" style=\"vertical-align: 0px;\"\/>. Wouldn&#8217;t it be memory intensive? Is there a better way to do it like with functional programming?&#8217; Now I have to show him a more advanced trick that requires some mental leaps (the ones necessary to\u00a0get\u00a0sophisticated at the MATLAB language):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">C = 167*bsxfun(@rdivide, a'+42, b+17)<\/pre>\n<p>This one liner does not save intermediate input values, so it&#8217;s memory efficient as well.<\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/help\/matlab\/ref\/bsxfun.html\">bsxfun<\/a>() is a function that takes two inputs (we call it a binary function) which <span style=\"text-decoration: underline;\">any<\/span> of them can be a matrix, vector or scalar. It will <span style=\"text-decoration: underline;\">conceptually<\/span> expand the dimensions\u00a0so the function handle (e.g. @rdivide) get to apply to all combinations as if the inputs are expanded (repeated)\u00a0to the\u00a0longer of each dimension supplied. I bet under the hood it&#8217;s just a pair of for-loops with the\u00a0loop increments managed so it doesn&#8217;t waste memory storing the intermediaries.<\/p>\n<p>In the example above, I have a column <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-fff7a67431e2915c040c90d7adc2fcc4_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#97;&#94;&#84;&#43;&#52;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"59\" style=\"vertical-align: -2px;\"\/> and a\u00a0row <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-9967ea3139389f64c93b214d77392c2e_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#98;&#43;&#49;&#55;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"47\" style=\"vertical-align: -2px;\"\/>. The output <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-ed12970f60569db1dfd9f13289854a0d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#67;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"14\" style=\"vertical-align: 0px;\"\/> is arranged as if\u00a0<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-fff7a67431e2915c040c90d7adc2fcc4_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#97;&#94;&#84;&#43;&#52;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"59\" style=\"vertical-align: -2px;\"\/> is copied right to meet the length of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-9967ea3139389f64c93b214d77392c2e_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#98;&#43;&#49;&#55;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"47\" style=\"vertical-align: -2px;\"\/>, and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-9967ea3139389f64c93b214d77392c2e_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#98;&#43;&#49;&#55;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"47\" style=\"vertical-align: -2px;\"\/> is copied down to meet the length of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-fff7a67431e2915c040c90d7adc2fcc4_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#97;&#94;&#84;&#43;&#52;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"59\" style=\"vertical-align: -2px;\"\/>.<\/p>\n<p>This involves two major concepts one needs to\u00a0program the\u00a0MATLAB way : vectorization and anonymous functions. Not something you&#8217;d tell a\u00a0day-zero beginner (probably scare them off), but showing them a\u00a0Ninja trick\u00a0after they understand\u00a0the beginner&#8217;s method might motivate them to learn the true power of MATLAB.<\/p>\n<hr \/>\n<p>Rik has pointed out in the comments below that TMW introduced <a href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/10\/24\/matlab-arithmetic-expands-in-r2016b\/\"><em>implicit expansion<\/em><\/a> that makes the bsxfun() obsolete. This is an excellent move (readability), despite it won&#8217;t police <a href=\"https:\/\/web.stanford.edu\/class\/ee103\/matrix_crimes.pdf\">matrix crimes<\/a> anymore so educators should teach about implicit expansion as the first thing as it&#8217;d be hard to debug is the user is not aware of the behavior. Thanks Rik!<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_41\" class=\"pvc_stats all  \" data-element-id=\"41\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A coworker whose background is in embedded systems (with a C background and no MATLAB at all), after hearing my rants that people are coding MATLAB like C using way more for-loops than necessary,\u00a0asked me if he has two vectors, &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2016\/07\/24\/matlab-fundamentals-vectorization\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_41\" class=\"pvc_stats all  \" data-element-id=\"41\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-matlab"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":7,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":901,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions\/901"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}