{"id":2846,"date":"2021-10-24T00:14:50","date_gmt":"2021-10-24T08:14:50","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=2846"},"modified":"2025-12-01T19:52:07","modified_gmt":"2025-12-02T03:52:07","slug":"matlab-repeating-arrays-elementwise-array-replication-interleaved-repmat","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/24\/matlab-repeating-arrays-elementwise-array-replication-interleaved-repmat\/","title":{"rendered":"MATLAB repeating arrays (elementwise array replication, interleaved &#8216;repmat&#8217;)"},"content":{"rendered":"\n<p>Since MATLAB R2015b, there&#8217;s a <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/1975772\/repeat-copies-of-array-elements-run-length-decoding-in-matlab\/29084077#29084077\" target=\"_blank\">new feature<\/a> called <code><strong>repelem(V, dim1, dim2, ...)<\/strong><\/code> which repeats each element by dimX times over dimension X. If N (dim1) is scalar, each V is uniformly repeated by N times. If N is a vector, it has to be the same length as V and each element of N says how many times the corresponding element in V is repeated.<\/p>\n\n\n\n<p>Here are some historical ways of doing it (as mentioned in <a href=\"https:\/\/www.ee.columbia.edu\/~marios\/matlab\/mtt.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">MATLAB array manipulation tips<\/a>)<\/p>\n\n\n\n<p>The scalar case (repeat uniformly) can be emulated by a Kronecker product multiplying everything with 1 (self):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kron(V, ones(N,1))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"648\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-7-1024x648.png\" alt=\"\" class=\"wp-image-2847\" srcset=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-7-1024x648.png 1024w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-7-300x190.png 300w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-7-768x486.png 768w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-7.png 1233w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Just replace all the elements b with 1 so we are left with elements of A repeating the way we wanted<\/figcaption><\/figure>\n\n\n\n<p>Kron method is conceptually smart but it has unnecessary arithmetic (multiply by 1). Nonetheless this method is reasonably fast until TMW finally developed a built-in function for it that outperforms all the tricks people have accumulated over decades.<\/p>\n\n\n\n<p>The vector case (each element is repeated a different number of times according to vector N) is basically decoding Run-Length Encoding (RLE), aka counts to placements, which you can download maturely written programs on MATLAB File Exchange (FEX). There are a bunch of <code>cumsum<\/code>\/<code>diff<\/code>\/<code>accumarray<\/code>\/<code>reshape <\/code>tricks but at the end of the day, they are RLE decoding in vectorized forms.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_2846\" class=\"pvc_stats all  \" data-element-id=\"2846\" 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><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Since MATLAB R2015b, there&#8217;s a new feature called repelem(V, dim1, dim2, &#8230;) which repeats each element by dimX times over dimension X. If N (dim1) is scalar, each V is uniformly repeated by N times. If N is a vector, &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/24\/matlab-repeating-arrays-elementwise-array-replication-interleaved-repmat\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2846\" class=\"pvc_stats all  \" data-element-id=\"2846\" 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":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-2846","post","type-post","status-publish","format-standard","hentry","category-matlab"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/2846","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=2846"}],"version-history":[{"count":12,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/2846\/revisions"}],"predecessor-version":[{"id":6833,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/2846\/revisions\/6833"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=2846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=2846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=2846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}