changeset 8970:b37a6c27c23f

simplify repmat * * *
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 13 Mar 2009 12:18:45 +0100
parents 3ecbc236e2e0
children 967a692ddfe2
files scripts/ChangeLog scripts/general/repmat.m
diffstat 2 files changed, 18 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,8 @@
+2009-03-13  Jaroslav Hajek  <highegg@gmail.com>
+
+	* general/repmat.m: Use subscript pairs rather than forming Kronecker
+	products.
+
 2009-03-11  Ben Abbott  <bpabbott@mac.com>
 
 	* plot/__go_draw_axes__.m: Unset the {x,y,z}ticks when initializing
--- a/scripts/general/repmat.m
+++ b/scripts/general/repmat.m
@@ -74,17 +74,21 @@
       x = reshape (x, m*p, n*q);
     endif
   else
-    aidx = size(a);
-    if (length(aidx) > length(idx))
-      idx = [idx, ones(1,length(aidx)-length(idx))];
-    elseif (length(aidx) < length(idx))
-      aidx = [aidx, ones(1,length(idx)-length(aidx))];
-    endif
-    cidx = cell (1, length (aidx));
+    aidx = size (a);
+    ## ensure matching size
+    idx(end+1:length (aidx)) = 1;
+    aidx(end+1:length (idx)) = 1;
+    ## create subscript array
+    cidx = cell (2, length (aidx));
     for i = 1:length (aidx)
-      cidx{i} = kron (ones (1, idx(i)), 1:aidx(i));
+      cidx{1,i} = ':';
+      cidx{2,i} = ones (1, idx (i));
     endfor
-    x = a (cidx{:});
+    aaidx = aidx;
+    # add singleton dims
+    aaidx(2,:) = 1;
+    a = reshape (a, aaidx(:));
+    x = reshape (a (cidx{:}), idx .* aidx);
   endif
 
 endfunction