The evolution together a basic computing functions find -matlab
Find useful, welcome to discuss mutual learning together - Follow Me
Find and index values of nonzero elements
grammar
- k = find(X)
- k = find (X) returns a vector, wherein each X comprises an array of non-zero elements linearly indexed .
- If X is a vector, then find returns a direction vector identical to X
- If X is a column vector multidimensional array, then find returns the result of the linear index
- If X does not contain non-zero elements or empty, then find an empty array
- k = find (X, n) k = find (X, n) returns the index of the first n X non-zero elements corresponding to
- k = find (X, n, direction) k = find (X, n, direction), wherein the direction is a 'last', X find the last non-zero elements n
- [Row, col] = find (___) using the input parameters of any preceding syntax Returns an array X non-zero elements of each row and column index
[Row, col, v] = find (___) returns a vector v, which comprises a non-zero elements of X
Examples
Find the matrix elements of zero and non-zero elements
X = [1 0 2; 0 1 1; 0 0 4]
X =
1 0 2
0 1 1
0 0 4
k = find(X)
k =
1
5
7
8
9
% 顺序是从上往下从左至右进行排列的
k2 = find(~X)
k2 =
2
3
4
6
Find the elements that meet certain criteria
X = magic(4)
X =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
k = find(X<10,5)
k =
2
3
4
5
7
X(k)
ans =
5
9
4
2
7
Element is equal to a specific value
- It is equal to the case of particular value using the "==" integer value
x = 1:2:20
x =
1 3 5 7 9 11 13 15 17 19
k = find(x==13)
k =
7
- To find the non-integer value, use the tolerance value based data. Otherwise, because the floating point rounding errors, the result sometimes is an empty matrix
y = 0:0.1:1
y =
Columns 1 through 7
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
Columns 8 through 11
0.7000 0.8000 0.9000 1.0000
k = find(y==0.3)
k =
Empty matrix: 1-by-0
k = find(abs(y-0.3) < 0.001)
k =
4
Looking for the last few nonzero project
X = magic(6);
X(1:2:end) = 0 % 索引从左上角往下后往右
X =
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
% 寻找最后几个非零元素的索引
k = find(X,4,'last')
k =
30
32
34
36
Looking to meet the multi-constraint
- Find the 4 × 4 matrix in the first three elements of more than 0 to less than 10. Two outputs designated, the row and column index is returned to element
X = [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
X =
18 3 1 11
8 10 11 3
9 14 6 1
4 3 15 21
[row,col] = find(X>0 & X<10,3)
row =
2
3
4
col =
1
1
1
And subscript values of nonzero elements
- Seeking 3 × 3 matrix of non-zero elements. Returns the output to the specified three rows superscripts, subscripts and element value column
X = [3 2 0; -5 0 7; 0 0 1]
X =
3 2 0
-5 0 7
0 0 1
[row,col,v] = find(X)
row =
1
2
1
2
3
col =
1
1
2
3
3
v =
3
-5
2
7
1