# HG changeset patch # User Rik # Date 1523400196 25200 # Node ID ff830f8e61ac800e3b831d6f8f253c93e5f19ec4 # Parent 6254e47f2c6c0172f4f011c3f6d3ca3ca1d40c6c nonzeros.m: Overhaul function for performance (bug #53605). * nonzeros.m: Change documentation to have return value 'v' for values and matrix input 'A'. Change function prototype to match documentation. Use issparse (A) to check input and choose between algorithms for performance. Add input validation BIST tests. diff -r 6254e47f2c6c -r ff830f8e61ac scripts/sparse/nonzeros.m --- a/scripts/sparse/nonzeros.m Tue Apr 10 13:37:09 2018 -0400 +++ b/scripts/sparse/nonzeros.m Tue Apr 10 15:43:16 2018 -0700 @@ -17,20 +17,24 @@ ## . ## -*- texinfo -*- -## @deftypefn {} {} nonzeros (@var{s}) -## Return a vector of the nonzero values of the sparse matrix @var{s}. +## @deftypefn {} {@var{v} =} nonzeros (@var{A}) +## Return a column vector of the nonzero values of the matrix @var{A}. ## @seealso{find, nnz} ## @end deftypefn -function t = nonzeros (s) +function v = nonzeros (A) if (nargin != 1) print_usage (); endif - [~, ~, t] = find (s); - - t = t(:); + if (issparse (A)) + [~, ~, v] = find (A); + v = v(:); + else + v = A(find (A)); + v = v(:); + endif endfunction @@ -39,3 +43,7 @@ %!assert (nonzeros ([1,2,3,0]), [1;2;3]) %!assert (nonzeros (sparse ([1,2;3,0])), [1;3;2]) %!assert (nonzeros (sparse ([1,2,3,0])), [1;2;3]) + +## Test input validation +%!error nonzeros () +%!error nonzeros (1, 2)