find

Returns the position of non-zero elements in a matrix.

Syntax

idx = find(x)

idx = find(x,n)

idx = find(x,n,dir)

[i,j] = find(...)

[i,j,v] = find(...)

Inputs

x
The matrix to search for non-zero elements.
Type: double | integer | logical | struct | cell
Dimension: scalar | vector | matrix
n
The maximum number of elements to return.
Type: integer
dir
The direction in which to start the search, either "first" or "last". "first" searches the matrix from the beginning, whereas "last" starts the search from the end of the matrix.
Type: string

Outputs

idx
A vector of the vector indices of the nonzero elements. To achieve this result, OML treats the matrix as if the columns of the given matrix are one vector.
Type: integer
Dimension: scalar | vector
i,j
The row and column indices of nonzero elements in the given matrix.
Type: integer
Dimension: scalar | vector
v
A vector of the found nonzero elements.
Type: double | int | log
Dimension: scalar | vector

Examples

Simple matrix input:
R = find([1,0;0,1])
R = [ 1 ; 4 ]
Simple matrix input with a maximum return of 1:
R = find([1,0;0,1],1)
R = 1
Simple matrix input with a maximum return of 1, starting the search from the beginning:
R = find([1,0;0,1],1,'first')
R = 1
Simple matrix input with a maximum return of 1, starting the search from the end:
R = find([1,0;0,1],1,'last')
R = 4
Two variable return with a matrix input:
[a,b]=find([0,1,0,-1])
a = [ 1 1 ]
b = [ 2 4 ]
Three variable return with a matrix input:
[a,b,c]=find([0,1,0,-1])
a = [ 1 1 ]
b = [ 2 4 ]
c = [ 1 -1 ]