find
Syntax
k = find(X,<n>,<dir>)returns a vector containing the linear indices of the first/last n nonzero elements of array X- n defaults to all matching elements
- dir may be
'first'or'last'; the default is'first'
[row,col] = find(___)returns the row and column subscripts of each nonzero element of array X[row,col,v] = find(___)additionally returns a vector v containing the nonzero values of X
Usage
A common pattern is to chain several logical operations to produce a logical array flagging the elements that satisfy some condition, then call find on that logical array to retrieve the indices of the matching elements—giving you direct access to them.
Example:
>> A = rand(3)
A =
0.5085 0.7948 0.8116
0.5108 0.6443 0.5328
0.8176 0.3786 0.3507
>> test = (A>0.3) & (A<0.7);
>> I = find(test);
>> A(I)
ans =
0.5085
0.5108
0.6443
0.3786
0.5328
0.3507