findFinding indices of non-zero elements
v = find(X)[r, c] = find(X)[r, c, x] = find(X)X is a numeric array.v is a vector containing linear indices of non-zero elements of X.r and c contain, respectively, the row and column indices of the non-zero elements. If X has more than 2 dimensions, X is first reshaped to a matrix with the same number of rows as the original X.x is equal to X(v).v = find(X, n)[r, c] = find(X, n)[r, c, x] = find(X, n)n non-zero elements are returned.n should be a positive integer scalar.X=randi(2, 3, 4)-2
v=find(X)
X =
-1.0000 0.0000 -1.0000 0.0000
0.0000 0.0000 0.0000 0.0000
0.0000 -1.0000 0.0000 -1.0000
v =
1.0000
6.0000
7.0000
12.000
X=randi(6,3,4)-1
[r,c,x]=find(X)
X =
1.0000 3.0000 1.0000 2.0000
4.0000 5.0000 5.0000 5.0000
4.0000 4.0000 3.0000 3.0000
r =
1.0000
2.0000
3.0000
1.0000
2.0000
3.0000
1.0000
2.0000
3.0000
1.0000
2.0000
3.0000
c =
1.0000
1.0000
1.0000
2.0000
2.0000
2.0000
3.0000
3.0000
3.0000
4.0000
4.0000
4.0000
x =
1.0000
4.0000
4.0000
3.0000
5.0000
4.0000
1.0000
5.0000
3.0000
2.0000
5.0000
3.0000
X has 3 dimensions, which is first reshaped to a matrix before finding indices of non-zero elements.X=randi(6,3,4,2)-1
[r,c,x]=find(X)
X(:, :, 1) =
0.0000 2.0000 3.0000 0.0000
1.0000 3.0000 2.0000 0.0000
0.0000 4.0000 1.0000 1.0000
X(:, :, 2) =
0.0000 2.0000 3.0000 3.0000
4.0000 4.0000 5.0000 4.0000
2.0000 4.0000 0.0000 5.0000
r =
2.0000
1.0000
2.0000
3.0000
1.0000
2.0000
3.0000
3.0000
2.0000
3.0000
1.0000
2.0000
3.0000
1.0000
2.0000
1.0000
2.0000
3.0000
c =
1.0000
2.0000
2.0000
2.0000
3.0000
3.0000
3.0000
4.0000
5.0000
5.0000
6.0000
6.0000
6.0000
7.0000
7.0000
8.0000
8.0000
8.0000
x =
1.0000
2.0000
3.0000
4.0000
3.0000
2.0000
1.0000
1.0000
4.0000
2.0000
2.0000
4.0000
4.0000
3.0000
5.0000
3.0000
4.0000
5.0000