Index: find.cc =================================================================== RCS file: /cvs/octave/src/DLD-FUNCTIONS/find.cc,v retrieving revision 1.14 diff -u -r1.14 find.cc --- find.cc 19 May 2006 05:32:18 -0000 1.14 +++ find.cc 26 Sep 2006 03:34:20 -0000 @@ -36,8 +36,15 @@ template octave_value_list -find_nonzero_elem_idx (const T& nda, int nargout) +find_nonzero_elem_idx (const T& nda, int nargout, octave_idx_type n_to_find, + int direction) { + /* Find any nonzero elements. + nda - an array to search for nonzero elements in + nargout - the number of output arguments + n_to_find - the number of elements to find (-1 for unlimited) + direction - the direction to search (1 == forward, -1 == reverse) + */ octave_value_list retval (((nargout == 0) ? 1 : nargout), Matrix ()); octave_idx_type count = 0; @@ -85,7 +92,16 @@ octave_idx_type i = 0; octave_idx_type j = 0; - for (octave_idx_type k = 0; k < nel; k++) + /* Set the starting element to the correct value based on the + direction to search */ + octave_idx_type k = 0; + if (direction == -1) + { + k = nel - 1; + } + + /* Do the searching for elements to return */ + while ((k < nel) && (k > -1) && (n_to_find != count)) { OCTAVE_QUIT; @@ -109,6 +125,7 @@ j++; } + k = k + direction; } } else if (scalar_arg) @@ -145,13 +162,17 @@ return retval; } -template octave_value_list find_nonzero_elem_idx (const NDArray&, int); +template octave_value_list find_nonzero_elem_idx (const NDArray&, int, + octave_idx_type, int); -template octave_value_list find_nonzero_elem_idx (const ComplexNDArray&, int); +template octave_value_list find_nonzero_elem_idx (const ComplexNDArray&, int, + octave_idx_type, int); DEFUN_DLD (find, args, nargout, "-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} find (@var{x})\n\ address@hidden {Loadable Function} {} find (@var{x}, @var{n})\n\ address@hidden {Loadable Function} {} find (@var{x}, @var{n}, @var{direction})\n\ Return a vector of indices of nonzero elements of a matrix. To obtain a\n\ single index for each matrix element, Octave pretends that the columns\n\ of a matrix form one long vector (like Fortran arrays are stored). For\n\ @@ -186,18 +207,51 @@ @result{} v = [ 3; 3 ]\n\ @end group\n\ @end example\n\ +\n\ +If two inputs are given, @var{n} indicates the number of elements to\n\ +find from the beginning of the matrix or vector.\n\ +\n\ +If three inputs are given, @var{direction} should be one of \"first\" or\n\ +\"last\" indicating that it should start counting found elements from the\n\ +first or last element.\n\ @end deftypefn") { octave_value_list retval; int nargin = args.length (); - if (nargin != 1 || nargout > 3) + if ((nargin > 3 || nargout > 3) || + (nargin < 1) || + ((nargin > 1) && (! args(2).is_scalar_type ())) || + ((nargin > 2) && (! args(3).is_string ()))) { print_usage (); return retval; } + /* Setup the default options */ + octave_idx_type n_to_find = -1; + if (nargin > 1) + { + n_to_find = static_cast (args(2).int_value ()); + } + + /* Direction to do the searching (1 == forward, -1 == reverse) */ + int direction = 1; + if (nargin > 2) + { + std::string s_arg = args(3).string_value (); + if (s_arg == "first") + direction = 1; + else if (s_arg == "last") + direction = -1; + else + { + print_usage (); + return retval; + } + } + octave_value arg = args(0); if (arg.is_real_type ()) @@ -205,21 +259,21 @@ NDArray nda = arg.array_value (); if (! error_state) - retval = find_nonzero_elem_idx (nda, nargout); + retval = find_nonzero_elem_idx (nda, nargout, n_to_find, direction); } else if (arg.is_complex_type ()) { ComplexNDArray cnda = arg.complex_array_value (); if (! error_state) - retval = find_nonzero_elem_idx (cnda, nargout); + retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); } else if (arg.is_string ()) { charNDArray cnda = arg.char_array_value (); if (! error_state) - retval = find_nonzero_elem_idx (cnda, nargout); + retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); } else {