find — Locate nonzero indices and selected values in column-major order.
find returns one-based double indices of nonzero elements. With three outputs it also returns selected values in their original class, including exact wide integers.
Syntax
idx = find(X)
idx = find(X, K)
idx = find(X, direction)
idx = find(X, K, direction)
[row, col] = find(X)
[row, col] = find(X, K)All supported find forms
idx = find(X)
idx = find(X, K)
idx = find(X, direction)
idx = find(X, K, direction)
[row, col] = find(X)
[row, col] = find(X, K)
[row, col] = find(X, direction)
[row, col] = find(X, K, direction)
[row, col, v] = find(X)
[row, col, v] = find(X, K)
[row, col, v] = find(X, direction)
[row, col, v] = find(X, K, direction)Inputs
X- Type
Any- Required
- Yes
- Description
- Input array to search.
K- Type
NumericScalar- Required
- Yes
- Description
- Maximum number of indices to return.
direction- Type
StringScalar- Required
- Yes
- Description
- RunMat-only direction selector: first or last.
direction- Type
StringScalar- Required
- Yes
- Default
"first"- Description
- Direction selector: `"first"` or `"last"`.
Returns
idx- Type
NumericArray- Description
- Linear indices of non-zero elements.
row- Type
NumericArray- Description
- Row subscripts of non-zero elements.
col- Type
NumericArray- Description
- Column subscripts of non-zero elements.
v- Type
Any- Description
- Values at the reported row/column locations.
Returned values from find depend on how many outputs the caller requests.
Errors
RunMat:find:InvalidInput- When
- Input type or option arguments are not valid for find.
- Message
find: invalid input arguments
RunMat:find:ProviderOutput- When
- GPU provider does not return expected output buffers for requested nargout.
- Message
find: provider output buffer mismatch
RunMat:find:ProviderError- When
- The owning provider fails to transfer or produce an admitted find result.
- Message
find: provider execution failed
RunMat:find:InternalError- When
- Internal tensor conversion/materialization fails while building outputs.
- Message
find: internal error
Selection and output shape
The optional positive integer K limits the matches. 'first' scans from the start; 'last' selects the final K matches but reports them in ascending order. A row-vector input gives row-oriented one-output indices; other inputs give a column. Two or three outputs are column vectors, with trailing N-D dimensions folded into the column subscript.
A zero scalar or empty matrix produces the empty matrix convention. Other empty row vectors retain row orientation. NaN and infinity count as nonzero; either nonzero complex component counts.
Compatibility and exactness
find(X,direction) is a RunMat-only convenience form; compatible direction selection supplies K. Typed-integer sparse values are likewise gated. All eight integer classes use authoritative storage for zero testing and the third value output.
Indices are doubles and reject values outside their exact range. The third output retains logical, single, complex, or integer storage as appropriate.
Resident execution
A capable binary64 provider can run find directly. Other resident inputs use owner-aware exact gathering and return each output to the same owner when the class can be represented. find is eager and is not fused.
Examples
Compile selection outputs and errors
A = [0 4 0; 7 0 9]; k = find(A); [r,c,v] = find(A); tail = find([1 0 2 3 0],2,'last'); direction = 'sideways'; caught = false; try; bad = find(A,1,direction); catch failure; caught = strcmp(failure.identifier,'RunMat:find:InvalidInput'); endReject an unknown direction
find([1 0], 1, 'sideways')Find matrix positions
A = [0 4 0; 7 0 9];
k = find(A)Expected output:
k = [2; 3; 6]Select the final matches
A = [1 0 2 3 0];
k = find(A,2,'last')Expected output:
k = [3 4]Preserve wide integer values
A = uint64([0 0x0020000000000001u64]);
[r,c,v] = find(A)Expected output:
r = 1, c = 2Using find with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how find changes the result.
Run a small find example, explain the result, then change one input and compare the output.
FAQ
What counts as nonzero?⌄
Exact nonzero values, NaN, and infinities; either component of a complex value can make it nonzero.
Does the value output preserve integer precision?⌄
Yes. It reads and returns authoritative integer storage without a double round trip.
Related Array functions
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how find is executed, line by line, in Rust.
- View the source for find in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.