find
find(X) returns indices of non zero elements in matrix X.
- find(X, n) returns the first n indices of non zero elements in X
- find(X, n, str) with str:
- 'first' returns the first n indices
- 'last' returns the last n indices
- [i, j] = find(X) returns rows and columns position of non zero elements in X
- [i, j, v] = find(X) returns rows and columns position of non zero elements in X, as well as their values
Examples
> X = [1 2; 0 3]# 1 2 0 8 > find(X)# 1 3 4 > [i j] = find(X) [i:3x1 double] [j:3x1 double] > i# 1 1 2 > j# 1 2 2 > [i j v] = find(X) [i:3x1 double] [j:3x1 double] [v:3x1 double] > v# 1 2 8 > x(i(1), j(1)) == v(1)# 1
> find([4 2 9 8 5 6] > 4, 3)# 3 4 5
> find([3 0 1 0 4 0 2], 2, 'last')# 5 7