changeset 25562:9277b77dd28f stable

repelem.m: Fix behavior when final repeat count is 0 (bug #54275). * repelem.m: Size intermediate variable idx to be one larger than may be necessary. After calculation, select only the valid range of idx without the extra temporary. Add BIST test.
author Rik <rik@octave.org>
date Mon, 09 Jul 2018 11:31:15 -0700
parents b116ea5a8d73
children a24b55da36ac 65a28dec405b
files scripts/general/repelem.m
diffstat 1 files changed, 7 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/repelem.m	Mon Jul 09 13:35:49 2018 -0400
+++ b/scripts/general/repelem.m	Mon Jul 09 11:31:15 2018 -0700
@@ -299,20 +299,19 @@
 
   else
     ## This works for a row or column vector.
-    ## idx2 output will be a row vector.
 
     ## Get ending position for each element item.
     idx_temp = cumsum (v);
 
-    ## Row vector with enough space for output
-    idx(1:idx_temp(end)) = 0;
-
     ## Set starting position of each element to 1.
-    idx(idx_temp(1:end-1) + 1) = 1;
+    idx(idx_temp + 1) = 1;
 
     ## Set starting position of each element to 1.
     idx(1) = 1;
 
+    ## Row vector with proper length for output
+    idx = idx(1:idx_temp(end));
+
     ## with prepared index
     idx = (find (v != 0))(cumsum (idx));
 
@@ -427,6 +426,9 @@
 %! assert (repelem ({1 0;0 -1}, 2, 3),
 %!         {1 1 1 0 0 0;1 1 1 0 0 0;0 0 0 -1 -1 -1;0 0 0 -1 -1 -1});
 
+%!test <*54275>
+%! assert (repelem (11:13, [1 3 0]), [11 12 12 12]);
+
 ## nargin <= 1 error tests
 %!error (repelem ());
 %!error (repelem ([]));