changeset 29196:3332f1964e59

maint: merge stable to default.
author Rik <rik@octave.org>
date Thu, 17 Dec 2020 17:57:50 -0800
parents 01e5c00a8609 (current diff) 900246a8bb37 (diff)
children 1a3cb2e1644a
files scripts/general/repelem.m
diffstat 1 files changed, 14 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/repelem.m	Thu Dec 17 15:58:23 2020 -0800
+++ b/scripts/general/repelem.m	Thu Dec 17 17:57:50 2020 -0800
@@ -226,7 +226,7 @@
     vector_r = ! (cellfun (@numel, varargin) == 1);
 
     ## 1. Check that all varargin are either scalars or vectors, not arrays.
-    ##    isvector gives true for scalars.
+    ##    isvector returns true for scalars so one test captures both inputs.
     if (! (isvector (varargin{1}) && (isvector (varargin{2}))))
       error ("repelem: R1 and R2 must be scalars or vectors");
 
@@ -237,13 +237,17 @@
     endif
 
     ## Create index arrays to pass to element.
-    ## (It is no slower passing to prepareIdx
-    ## than checking and doing scalars directly.)
+    ## (It is no slower to call prepareIdx than to check and do scalars
+    ## directly.)
     idx1 = prepareIdx (varargin{1}, xsz(1));
     idx2 = prepareIdx (varargin{2}, xsz(2));
 
-    ## The ":" at the end takes care of any x dimensions > 2.
-    retval = x(idx1, idx2, :);
+    if (issparse (x))
+      retval = x(idx1, idx2);
+    else
+      ## The ":" at the end takes care of any x dimensions > 2.
+      retval = x(idx1, idx2, :);
+    endif
 
   else  # (nargin > 3)
 
@@ -437,6 +441,11 @@
 %!test <*54275>
 %! assert (repelem (11:13, [1 3 0]), [11 12 12 12]);
 
+%!test <*59705>
+%! xs = sparse (magic (3));
+%! assert (repelem (xs, 1, 2), ...
+%!         sparse ([8,8,1,1,6,6; 3,3,5,5,7,7; 4,4,9,9,2,2]));
+
 ## nargin <= 1 error tests
 %!error <Invalid call> repelem ()
 %!error <Invalid call> repelem (1)