diff scripts/general/repmat.m @ 11318:d7ea780b036f

repmat: handle special case of replicating scalar using index vector containing zeros
author John W. Eaton <jwe@octave.org>
date Tue, 07 Dec 2010 13:24:20 -0500
parents a40e32927b3a
children c776f063fefe
line wrap: on
line diff
--- a/scripts/general/repmat.m	Tue Dec 07 12:57:55 2010 -0500
+++ b/scripts/general/repmat.m	Tue Dec 07 13:24:20 2010 -0500
@@ -61,8 +61,12 @@
 
   if (numel (a) == 1)
     ## optimize the scalar fill case.
-    x(1:prod (idx)) = a;
-    x = reshape (x, idx);
+    if (any (idx == 0))
+      x = resize (a, idx);
+    else
+      x(1:prod (idx)) = a;
+      x = reshape (x, idx);
+    endif
   elseif (ndims (a) == 2 && length (idx) < 3)
     if (issparse (a))
       x = kron (ones (idx), a);
@@ -136,3 +140,21 @@
 %!assert (size (repmat (".", -1, 1)), [0, 1]);
 %!assert (size (repmat (".", 1, -1)), [1, 0]);
 %!error (size (repmat (".", -1, -1)));
+
+%!assert (size (repmat (1, [1, 0])), [1, 0]);
+%!assert (size (repmat (1, [5, 0])), [5, 0]);
+%!assert (size (repmat (1, [0, 1])), [0, 1]);
+%!assert (size (repmat (1, [0, 5])), [0, 5]);
+
+%!shared x
+%! x = struct ("a", [], "b", []);
+%!assert (size (repmat (x, [1, 0])), [1, 0]);
+%!assert (size (repmat (x, [5, 0])), [5, 0]);
+%!assert (size (repmat (x, [0, 1])), [0, 1]);
+%!assert (size (repmat (x, [0, 5])), [0, 5]);
+
+%!assert (size (repmat ({1}, [1, 0])), [1, 0]);
+%!assert (size (repmat ({1}, [5, 0])), [5, 0]);
+%!assert (size (repmat ({1}, [0, 1])), [0, 1]);
+%!assert (size (repmat ({1}, [0, 5])), [0, 5]);
+