changeset 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 2da532d0f41c
children f8e97e9a9301
files scripts/ChangeLog scripts/general/repmat.m
diffstat 2 files changed, 29 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Dec 07 12:57:55 2010 -0500
+++ b/scripts/ChangeLog	Tue Dec 07 13:24:20 2010 -0500
@@ -1,3 +1,8 @@
+2010-12-07  John W. Eaton  <jwe@octave.org>
+
+	* general/repmat.m: Handle special case of replicating scalar
+	using an index vector containing zeros.  Bug #31775.
+
 2010-12-06  Rik  <octave@nomad.inbox5.com>
 
 	* plot/plot.m: Eliminate present tense in first sentence of docstring.
--- 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]);
+