Mercurial > octave
changeset 29195:900246a8bb37 stable
replem.m: Fix operations with sparse matrices (bug #59705).
* repelem.m: Check for sparse matrices in 2-D case and do not
use third, unsupported, index operator. Add BIST test for bug #59705.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 17 Dec 2020 17:57:20 -0800 |
parents | b2d5ee958d7f |
children | 3332f1964e59 6695a6bf4836 |
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:57:51 2020 -0800 +++ b/scripts/general/repelem.m Thu Dec 17 17:57:20 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 (repelem ()); %!error (repelem ([]));