47 idx = nda.
find (n_to_find, direction == -1);
68 jdx.xelem (i) = idx.
xelem (i) / nr;
109 else if (direction > 0)
114 if (v.
cidx (j) == 0 && v.
cidx (j+1) != 0)
116 if (v.
cidx (j+1) >= n_to_find)
128 if (v.
cidx (j) == nz && v.
cidx (j-1) != nz)
130 if (nz - v.
cidx (j-1) >= n_to_find)
138 count = (n_to_find > v.
cidx (end_nc) - v.
cidx (start_nc) ?
139 v.
cidx (end_nc) - v.
cidx (start_nc) : n_to_find);
157 Matrix idx (result_nr, result_nc);
159 Matrix i_idx (result_nr, result_nc);
160 Matrix j_idx (result_nr, result_nc);
172 if (direction < 0 && i < nz - count)
174 i_idx(cx) =
static_cast<double> (v.
ridx (i) + 1);
175 j_idx(cx) =
static_cast<double> (j + 1);
176 idx(cx) = j * nr + v.
ridx (i) + 1;
187 if ((nr == 0 && nc == 0) || (nr == 1 && nc == 1))
242 if (n_to_find < 0 || n_to_find >= nc)
247 else if (direction > 0)
254 start_nc = nc - n_to_find;
272 i_idx(k) =
static_cast<double> (1+i);
273 j_idx(k) =
static_cast<double> (1+j);
274 idx(k) = j * nc + i + 1;
285 if ((nr == 0 && nc == 0) || (nr == 1 && nc == 1))
330 @deftypefn {Built-in Function} {@var{idx} =} find (@var{x})\n\
331 @deftypefnx {Built-in Function} {@var{idx} =} find (@var{x}, @var{n})\n\
332 @deftypefnx {Built-in Function} {@var{idx} =} find (@var{x}, @var{n}, @var{direction})\n\
333 @deftypefnx {Built-in Function} {[i, j] =} find (@dots{})\n\
334 @deftypefnx {Built-in Function} {[i, j, v] =} find (@dots{})\n\
335 Return a vector of indices of nonzero elements of a matrix, as a row if\n\
336 @var{x} is a row vector or as a column otherwise.\n\
338 To obtain a single index for each matrix element, Octave pretends that the\n\
339 columns of a matrix form one long vector (like Fortran arrays are stored).\n\
345 @result{} [ 1; 4 ]\n\
349 If two inputs are given, @var{n} indicates the maximum number of elements to\n\
350 find from the beginning of the matrix or vector.\n\
352 If three inputs are given, @var{direction} should be one of\n\
353 @qcode{\"first\"} or @qcode{\"last\"}, requesting only the first or last\n\
354 @var{n} indices, respectively. However, the indices are always returned in\n\
357 If two outputs are requested, @code{find} returns the row and column\n\
358 indices of nonzero elements of a matrix. For example:\n\
362 [i, j] = find (2 * eye (2))\n\
363 @result{} i = [ 1; 2 ]\n\
364 @result{} j = [ 1; 2 ]\n\
368 If three outputs are requested, @code{find} also returns a vector\n\
369 containing the nonzero values. For example:\n\
373 [i, j, v] = find (3 * eye (2))\n\
374 @result{} i = [ 1; 2 ]\n\
375 @result{} j = [ 1; 2 ]\n\
376 @result{} v = [ 3; 3 ]\n\
380 Note that this function is particularly useful for sparse matrices, as\n\
381 it extracts the nonzero elements as vectors, which can then be used to\n\
382 create the original matrix. For example:\n\
387 [i, j, v] = find (a);\n\
388 b = sparse (i, j, v, sz(1), sz(2));\n\
391 @seealso{nonzeros}\n\
396 int nargin = args.
length ();
398 if (nargin > 3 || nargin < 1)
408 double val = args(1).scalar_value ();
412 error (
"find: N must be a non-negative integer");
425 std::string s_arg = args(2).string_value ();
429 if (s_arg ==
"first")
431 else if (s_arg ==
"last")
437 error (
"find: DIRECTION must be \"first\" or \"last\"");
452 n_to_find, direction);
454 else if (nargout <= 1 && n_to_find == -1 && direction == 1)
466 n_to_find, direction);
471 #define DO_INT_BRANCH(INTT) \
472 else if (arg.is_ ## INTT ## _type ()) \
474 INTT ## NDArray v = arg.INTT ## _array_value (); \
477 retval = find_nonzero_elem_idx (v, nargout, \
478 n_to_find, direction);\
502 n_to_find, direction);
510 n_to_find, direction);
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
bool is_empty(void) const
octave_value_list find_nonzero_elem_idx(const Array< T > &nda, int nargout, octave_idx_type n_to_find, int direction)
octave_idx_type cols(void) const
charNDArray char_array_value(bool frc_str_conv=false) const
bool is_real_type(void) const
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
octave_idx_type rows(void) const
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
octave_idx_type rows(void) const
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
octave_idx_type length(void) const
octave_idx_type cols(void) const
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
bool is_perm_matrix(void) const
octave_idx_type * cidx(void)
boolNDArray bool_array_value(bool warn=false) const
octave_idx_type rows(void) const
octave_idx_type nnz(void) const
idx_vector index_vector(bool require_integers=false) const
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
FloatNDArray float_array_value(bool frc_str_conv=false) const
idx_vector unmask(void) const
bool is_sparse_type(void) const
bool is_bool_type(void) const
const Array< octave_idx_type > & col_perm_vec(void) const
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
bool is_string(void) const
SparseBoolMatrix sparse_bool_matrix_value(bool warn=false) const
bool is_complex_type(void) const
void resize(const dim_vector &dv, const T &rfv)
#define panic_impossible()
SparseComplexMatrix sparse_complex_matrix_value(bool frc_str_conv=false) const
#define DO_INT_BRANCH(INTT)
static octave_idx_type find(octave_idx_type i, octave_idx_type *pp)
T & xelem(octave_idx_type n)
Handles the reference counting for all the derived classes.
octave_idx_type length(void) const
Number of elements in the array.
NDArray array_value(bool frc_str_conv=false) const
octave_idx_type * ridx(void)
Array< octave_idx_type > find(octave_idx_type n=-1, bool backward=false) const
Find indices of (at most n) nonzero elements.
PermMatrix perm_matrix_value(void) const
SparseMatrix sparse_matrix_value(bool frc_str_conv=false) const
bool is_single_type(void) const
Array< T > index(const idx_vector &i) const
Indexing without resizing.
bool is_integer_type(void) const