octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #54275] repelem error if a repeat instruction


From: anonymous
Subject: [Octave-bug-tracker] [bug #54275] repelem error if a repeat instruction is a vector ends with 0
Date: Mon, 9 Jul 2018 05:38:55 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0

URL:
  <http://savannah.gnu.org/bugs/?54275>

                 Summary: repelem error if a repeat instruction is a vector
ends with 0
                 Project: GNU Octave
            Submitted by: None
            Submitted on: Mon 09 Jul 2018 09:38:53 AM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Unexpected Error
                  Status: None
             Assigned to: None
         Originator Name: 
        Originator Email: address@hidden
             Open/Closed: Open
         Discussion Lock: Any
                 Release: 4.4.0
        Operating System: Any

    _______________________________________________________

Details:

For example,

octave:1>  repelem(11:13, [1 3 0])
error: index (3): out of bound 2
error: called from
    repelem>prepareIdx at line 317 column 9
    repelem at line 204 column 12



One possible solution is as follow.
(Besides, note that the comment "idx2 output will be a row vector" is wrong;
in fact the output can be either row or column vector.)

--- a/scripts/general/repelem.m
+++ b/scripts/general/repelem.m
@@ -299,7 +299,9 @@
 
   else
     ## This works for a row or column vector.
-    ## idx2 output will be a row vector.
+
+    ## Handle special case if v ends with 0.
+    v(end+1) = 1;
 
     ## Get ending position for each element item.
     idx_temp = cumsum (v);
@@ -316,6 +318,9 @@
     ## with prepared index
     idx = (find (v != 0))(cumsum (idx));
 
+    ## Handle special case if v ends with 0.
+    idx(end) = [];
+
   endif
 
 endfunction


Another solution is to use built-in function "repelems" directly.

--- a/scripts/general/repelem.m
+++ b/scripts/general/repelem.m
@@ -299,22 +299,10 @@
 
   else
     ## This works for a row or column vector.
-    ## idx2 output will be a row vector.
+    ## idx 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;
-
-    ## Set starting position of each element to 1.
-    idx(1) = 1;
-
-    ## with prepared index
-    idx = (find (v != 0))(cumsum (idx));
+    idx_temp = 1:numel (v);
+    idx = repelems (idx_temp, [idx_temp; v(:).']);
 
   endif
 



And below is some proposed test case.

--- a/scripts/general/repelem.m
+++ d/scripts/general/repelem.m
@@ -341,6 +341,7 @@
 %!assert (repelem ([-1 0 1], [1 2 1]), [-1 0 0 1])
 %!assert (repelem ([-1 0 1]', [1 2 1]), [-1; 0; 0; 1])
 %!assert (repelem ([1 2 3 4 5]', [2 1 0 1 2]), [1 1 2 4 5 5]')
+%!assert (repelem ([1 2 3 4 5]', [2 1 2 1 0]), [1 1 2 3 3 4]')
 
 ## nargin == 3 tests
 %!assert (repelem ([1 0;0 -1], 2, 3),
@@ -384,6 +385,7 @@
 %!               1 1 1 1 1 1 0 0 0 1 1 1
 %!               1 1 1 1 1 1 0 0 0 1 1 1]));
 %!assert (repelem ([1 2 3 4 5], 2,[2 1 2 0 2]), [1 1 2 3 3 5 5;1 1 2 3 3 5
5])
+%!assert (repelem ([1 2 3 4 5], 2,[2 1 2 2 0]), [1 1 2 3 3 4 4;1 1 2 3 3 4
4])
 %
 ## nargin > 3 tests
 %!assert (repelem ([1 0;0 -1], 2, 3, 4), ...
@@ -409,6 +411,7 @@
 %!               -1 -1 0 0 0  1  1  1  1
 %!               -1 -1 0 0 0  1  1  1  1]));
 %!assert (repelem ([1 2 3;4 5 6],[0 2],2,2), repmat([4 4 5 5 6 6],2,1,2))
+%!assert (repelem ([1 2 3;4 5 6],[2 0],2,2), repmat([1 1 2 2 3 3],2,1,2))
 
 ## test with structures
 %!test





    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?54275>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]