find - Find indices and values of nonzero elements - MATLAB (original) (raw)
Find indices and values of nonzero elements
Syntax
Description
[k](#budqulo-k) = find([X](#budqulo-X))
returns a vector containing the linear indices of each nonzero element in arrayX
.
- If
X
is a vector, thenfind
returns a vector with the same orientation asX
. - If
X
is a multidimensional array, thenfind
returns a column vector of the linear indices of the result.
[k](#budqulo-k) = find([X](#budqulo-X),[n](#budqulo-n))
returns the first n
indices corresponding to the nonzero elements in X
.
[k](#budqulo-k) = find([X](#budqulo-X),[n](#budqulo-n),[direction](#budqulo-direction))
, where direction
is 'last'
, finds the last n
indices corresponding to nonzero elements in X
. The default for direction
is 'first'
, which finds the first n
indices corresponding to nonzero elements.
[[row](#budqulo-row),[col](#budqulo-col)] = find(___)
returns the row and column subscripts of each nonzero element in array X
using any of the input arguments in previous syntaxes.
[[row](#budqulo-row),[col](#budqulo-col),[v](#budqulo-v)] = find(___)
also returns vector v
, which contains the nonzero elements of X
.
Examples
Find the nonzero elements in a 3-by-3 matrix.
X = [1 0 2; 0 1 1; 0 0 4]
X = 3×3
1 0 2
0 1 1
0 0 4
Use the logical not
operator on X
to locate the zeros.
Find the first five elements that are less than 10 in a 4-by-4 magic square matrix.
X = 4×4
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
View the corresponding elements of X
.
To find a specific integer value, use the ==
operator. For instance, find the element equal to 13
in a 1-by-10 vector of odd integers.
x = 1×10
1 3 5 7 9 11 13 15 17 19
To find a noninteger value, use a tolerance value based on your data. Otherwise, the result is sometimes an empty matrix due to floating-point roundoff error.
y = 1×11
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
k =
1×0 empty double row vector
k = find(abs(y-0.3) < 0.001)
Create a 6-by-6 magic square matrix with all of the odd-indexed elements equal to zero.
X = magic(6); X(1:2:end) = 0
X = 6×6
0 0 0 0 0 0
3 32 7 21 23 25
0 0 0 0 0 0
8 28 33 17 10 15
0 0 0 0 0 0
4 36 29 13 18 11
Locate the last four nonzeros.
Find the first three elements in a 4-by-4 matrix that are greater than 0
and less than 10
. Specify two outputs to return the row and column subscripts to the elements.
X = [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
X = 4×4
18 3 1 11
8 10 11 3
9 14 6 1
4 3 15 21
[row,col] = find(X>0 & X<10,3)
The first instance is X(2,1)
, which is 8
.
Find the nonzero elements in a 3-by-3 matrix. Specify three outputs to return the row subscripts, column subscripts, and element values.
X = [3 2 0; -5 0 7; 0 0 1]
X = 3×3
3 2 0
-5 0 7
0 0 1
Find the nonzero elements in a 4-by-2-by-3 array. Specify two outputs, row
and col
, to return the row and column subscripts of the nonzero elements. When the input is a multidimensional array (N > 2
), find
returns col
as a linear index over the N-1
trailing dimensions of X
.
X = zeros(4,2,3); X([1 12 19 21]) = 1
X = X(:,:,1) =
1 0
0 0
0 0
0 0
X(:,:,2) =
0 0
0 0
0 0
1 0
X(:,:,3) =
0 1
0 0
1 0
0 0
Input Arguments
Input array, specified as a scalar, vector, matrix, or multidimensional array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
Complex Number Support: Yes
Number of nonzeros to find, specified as a positive integer scalar. By default, find(X,n)
looks for the first n
nonzero elements in X
.
Search direction, specified as the string 'first'
or 'last'
. Look for the last n
nonzero elements in X
using find(X,n,'last')
.
Output Arguments
Indices to nonzero elements, returned as a vector.
- If
X
is a row vector, thenk
is also a row vector. Otherwise,k
is a column vector. k
is an empty row vector or empty column vector whenX
is an empty array or has no nonzero elements.find
uses the convention thatk
is an empty matrix[]
whenX
is an empty matrix[]
.
You can return the nonzero values in X
using X(k)
.
Row subscripts, returned as a vector. Together, row
and col
specify the X(row,col)
subscripts corresponding to the nonzero elements in X
.
Column subscripts, returned as a vector. Together, row
and col
specify the X(row,col)
subscripts corresponding to the nonzero elements in X
.
If X
is a multidimensional array with N > 2
, then col
is a linear index over the N-1
trailing dimensions of X
. This preserves the relation X(row(i),col(i)) == v(i)
.
Nonzero elements of X
, returned as a vector.
More About
A linear index allows use of a single subscript to index into an array, such as A(k)
. MATLAB® treats the array as a single column vector with each column appended to the bottom of the previous column. Thus, linear indexing numbers the elements in the columns from top to bottom, left to right.
For example, consider a 3-by-3 matrix. You can reference the A(2,2)
element with A(5)
, and the A(2,3)
element with A(8)
. The linear index changes depending on the size of the array; A(5)
returns a differently located element for a 3-by-3 matrix than it does for a 4-by-4 matrix.
The sub2ind
and ind2sub
functions are useful in converting between subscripts and linear indices.
Tips
- To find array elements that meet a condition, use
find
in conjunction with a relational expression. For example,find(X<5)
returns the linear indices to the elements inX
that are less than5
. - To directly find the elements in
X
that satisfy the conditionX<5
, useX(X<5)
. Avoid function calls likeX(find(X<5))
, which unnecessarily usefind
on a logical matrix. - When you execute
find
with a relational operation likeX>1
, it is important to remember that the result of the relational operation is a logical matrix of ones and zeros. For example, the command[row,col,v] = find(X>1)
returns a column vector of logical1
(true
) values forv
. - The row and column subscripts,
row
andcol
, are related to the linear indices ink
byk = sub2ind(size(X),row,col)
.
Extended Capabilities
Thefind
function supports tall arrays with the following usage notes and limitations:
X
must be a tall column vector.
For more information, see Tall Arrays.
Usage notes and limitations:
- If a variable-size input becomes a row vector at run time, then code generation ends with an error. This limitation does not apply when the input is scalar or is a variable-length row vector.
- For variable-size inputs, the shape of empty outputs (0-by-0, 0-by-1, or 1-by-0) depends on the upper bounds of the size of the input. When the input array is a scalar or [] at run time, the output might not match MATLAB. If the input is a variable-length row vector, then the size of an empty output is 1-by-0. Otherwise, the size is 0-by-1.
- The generated code always returns a variable-length vector. Even when you provide the output vector
k
, the output is not fixed-size because the output can contain fewer thank
elements. For example,find(x,1)
returns a variable-length vector with one or zero elements.
The find
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a