# HG changeset patch # User John W. Eaton # Date 1291746260 18000 # Node ID d7ea780b036f30610a6cbf96358e9d43c919be7b # Parent 2da532d0f41ce2a95a1f121967ed0358511c1f35 repmat: handle special case of replicating scalar using index vector containing zeros diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2010-12-07 John W. Eaton + + * general/repmat.m: Handle special case of replicating scalar + using an index vector containing zeros. Bug #31775. + 2010-12-06 Rik * plot/plot.m: Eliminate present tense in first sentence of docstring. diff --git a/scripts/general/repmat.m b/scripts/general/repmat.m --- a/scripts/general/repmat.m +++ b/scripts/general/repmat.m @@ -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]); +