changeset 25197:ff830f8e61ac

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.
author Rik <rik@octave.org>
date Tue, 10 Apr 2018 15:43:16 -0700
parents 6254e47f2c6c
children 5251de3c34d7
files scripts/sparse/nonzeros.m
diffstat 1 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 ## <https://www.gnu.org/licenses/>.
 
 ## -*- 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)