find

Returns the position of non-zero elements in a matrix. If two returns are requested find returns the row and column indices of nonzero elements of 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 find non-zero elements in.
Any valid scalar | vector | matrix
Type: double | integer | logical | struct | cell
Dimension: scalar | vector | matrix
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.
Any valid scalar | vector | matrix
Type: double | integer | logical | struct | cell
Dimension: scalar | vector | matrix

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.
Any valid scalar | vector | matrix
Type: double | integer | logical | struct | cell
Dimension: scalar | vector | matrix
i,j
The indices of nonzero elements in the given matrix.
Any valid scalar | vector | matrix
Type: double | integer | logical | struct | cell
Dimension: scalar | vector | matrix
v
A vector of the found nonzero elements.
Any valid scalar | vector | matrix
Type: double | integer | logical | struct | cell
Dimension: scalar | vector | matrix

Examples

Simple matrix input:

R = find([1,0;0,1])
R = [ 1 ; 4 ]

Simple matrix input with max return of 1:

R = find([1,0;0,1],1)
R = 1

Simple matrix input with max return of 1, starting search from beginning:

R = find([1,0;0,1],1,'first')
R = 1

Simple matrix input with max return of 1, starting search from 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 ]