47 idx = nda.
find (n_to_find, direction == -1);
67 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);
148 bool scalar_arg =
false;
155 scalar_arg = (v.
columns () == 1);
158 Matrix idx (result_nr, result_nc);
160 Matrix i_idx (result_nr, result_nc);
161 Matrix j_idx (result_nr, result_nc);
174 if (direction < 0 && i < nz - count)
176 i_idx(cx) =
static_cast<double> (v.
ridx (i) + 1);
177 j_idx(cx) =
static_cast<double> (j + 1);
178 idx(cx) = j * nr + v.
ridx (i) + 1;
238 if (n_to_find < 0 || n_to_find >= nc)
244 else if (direction > 0)
251 start_nc = nc - n_to_find;
255 bool scalar_arg = (v.
rows () == 1 && v.
cols () == 1);
273 i_idx(k) =
static_cast<double> (1+i);
274 j_idx(k) =
static_cast<double> (1+j);
275 idx(k) = j * nc + i + 1;
288 i_idx(koff) =
static_cast<double> (1+i);
289 j_idx(koff) =
static_cast<double> (1+j);
290 idx(koff) = j * nc + i + 1;
337 @deftypefn {Built-in Function} {@var{idx} =} find (@var{x})\n\
338 @deftypefnx {Built-in Function} {@var{idx} =} find (@var{x}, @var{n})\n\
339 @deftypefnx {Built-in Function} {@var{idx} =} find (@var{x}, @var{n}, @var{direction})\n\
340 @deftypefnx {Built-in Function} {[i, j] =} find (@dots{})\n\
341 @deftypefnx {Built-in Function} {[i, j, v] =} find (@dots{})\n\
342 Return a vector of indices of nonzero elements of a matrix, as a row if\n\
343 @var{x} is a row vector or as a column otherwise. To obtain a single index\n\
344 for each matrix element, Octave pretends that the columns of a matrix form\n\
345 one long vector (like Fortran arrays are stored). For example:\n\
350 @result{} [ 1; 4 ]\n\
354 If two outputs are requested, @code{find} returns the row and column\n\
355 indices of nonzero elements of a matrix. For example:\n\
359 [i, j] = find (2 * eye (2))\n\
360 @result{} i = [ 1; 2 ]\n\
361 @result{} j = [ 1; 2 ]\n\
365 If three outputs are requested, @code{find} also returns a vector\n\
366 containing the nonzero values. For example:\n\
370 [i, j, v] = find (3 * eye (2))\n\
371 @result{} i = [ 1; 2 ]\n\
372 @result{} j = [ 1; 2 ]\n\
373 @result{} v = [ 3; 3 ]\n\
377 If two inputs are given, @var{n} indicates the maximum number of\n\
378 elements to find from the beginning of the matrix or vector.\n\
380 If three inputs are given, @var{direction} should be one of\n\
381 @qcode{\"first\"} or @qcode{\"last\"}, requesting only the first or last\n\
382 @var{n} indices, respectively. However, the indices are always returned in\n\
385 Note that this function is particularly useful for sparse matrices, as\n\
386 it extracts the non-zero elements as vectors, which can then be used to\n\
387 create the original matrix. For example:\n\
392 [i, j, v] = find (a);\n\
393 b = sparse (i, j, v, sz(1), sz(2));\n\
396 @seealso{nonzeros}\n\
401 int nargin = args.
length ();
403 if (nargin > 3 || nargin < 1)
413 double val = args(1).scalar_value ();
417 error (
"find: N must be a non-negative integer");
430 std::string s_arg = args(2).string_value ();
434 if (s_arg ==
"first")
436 else if (s_arg ==
"last")
442 error (
"find: DIRECTION must be \"first\" or \"last\"");
457 n_to_find, direction);
459 else if (nargout <= 1 && n_to_find == -1 && direction == 1)
471 n_to_find, direction);
476 #define DO_INT_BRANCH(INTT) \
477 else if (arg.is_ ## INTT ## _type ()) \
479 INTT ## NDArray v = arg.INTT ## _array_value (); \
482 retval = find_nonzero_elem_idx (v, nargout, \
483 n_to_find, direction);\
507 n_to_find, direction);
515 n_to_find, direction);