赞
踩
Find 这个函数用处也挺大的,这几天看很多程序都见到这一函数,今天要好好给阐述,了解下
这个函数是为了找到矩阵或者是数组,向量中的非零元素。下面一大段英文没耐心看。看看例子就行了。
第一个用法是
ndices = find(X)
X = [1 0 4 -3 0 0 0 8 6];
indices = find(X)
返回x中非零元素的序号从1开始
indices =
1 3 4 8 9
若x是个矩阵呢?
x= [1 0 ;1 5 ;4 0 ]
find(x)得到的是
1
2
3
5
是一列矢量,非零元素的序号是按一列一列的算
第二个用法
ind = find(X, k)
ind = find(X, k, 'first')
ind = find(X, k, 'last')
这三个是找前K个非零元素,前两式功能一致,第三式指从最后一个元素算起。
重点说一下
第三个用法
[row,col] = find(X, ...)
[row,col,v] = find(X, ...)
例如
X = [3 2 0; -5 0 7; 0 0 1];
[r,c,v] = find(X>2)
r =1
2
c =1
3
v =1
1
这样看看不出什么?看下式就比较明了。
1 2 1 就是说矩阵中第一行第二列的元素满足X>2为真用1来表示。余下的类似。
1 3 1
1 1 1
[r,c,v]
要注意一下这一用法
X = [3 2 0; -5 0 7; 0 0 1];
[r,c,v]= find(X) 这个得到的v是X中非零的元素,要注意这个区别
r =
1
2
1
2
3
c =
1
1
2
3
3
v =
3
-5
2
7
1
Ps:如果你想求出矩阵中满足某种特定要求的元素,你会想到用find,但这并不是一个好方法
有更加简便的方法如要找f1中小于等于 t的元素。用下式就能够做到
r1=f1(f1<=t);
求矩阵所有元素的均值也不要像C语言那样把所有一个个都加起来利用下式temp1=mean(r1(:));
find
Find indices and values of nonzero elements
ind = find(X)
ind = find(X, k)
ind = find(X, k, 'first')
ind = find(X, k, 'last')
[row,col] = find(X, ...)
[row,col,v] = find(X, ...)
ind = find(X)locates all nonzero elements of array X, and returns the linear indices ofthose elements in vector ind. If X is a row vector, then ind is a row vector;otherwise, ind is a column vector. If X contains no nonzero elements or is anempty array, then ind is an empty array.
ind = find(X, k) or ind = find(X, k, 'first') returns at most the first k indicescorresponding to the nonzero entries of X. k must be a positive integer, but itcan be of any numeric data type.
ind = find(X, k,'last') returns at most the last k indices corresponding to the nonzero entriesof X.
[row,col] = find(X, ...) returns the rowand column indices of the nonzero entries in the matrix X. This syntax isespecially useful when working with sparse matrices. If X is an N-dimensionalarray with N > 2, col contains linear indices for the columns. For example,for a 5-by-7-by-3 array X with a nonzero element at X(4,2,3), find returns 4 inrow and 16 in col. That is, (7 columns in page 1) + (7 columns in page 2) + (2columns in page 3) = 16.
[row,col,v] =find(X, ...) returns a column or row vector v of the nonzero entries in X, aswell as row and column indices. If X is a logical expression, then v is alogical array. Output v contains the non-zero elements of the logical arrayobtained by evaluating the expression X. For example,
A= magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
[r,c,v]= find(A>10);
r', c', v'
ans =
1 2 4 4 1 3
ans =
1 2 2 3 4 4
ans =
1 1 1 1 1 1
Here the returned vector v is a logicalarray that contains the nonzero elements of N where
N=(A>10)
X = [1 0 4 -3 0 0 0 8 6];
indices = find(X)
returns linear indices for the nonzeroentries of X.
indices =
1 3 4 8 9
You can use a logical expression to defineX. For example,
find(X > 2)
returns linear indices corresponding to theentries of X that are greater than 2.
ans =
3 8 9
The following find command
X = [3 2 0; -5 0 7; 0 0 1];
[r,c,v] = find(X)
returns a vector of row indices of thenonzero entries of X
r =
1
2
1
2
3
a vector of column indices of the nonzeroentries of X
c =
1
1
2
3
3
and a vector containing the nonzero entriesof X.
v =
3
-5
2
7
1
The expression
X = [3 2 0; -5 0 7; 0 0 1];
[r,c,v] = find(X>2)
returns a vector of row indices of thenonzero entries of N where N=(X>2)
r =
1
2
a vector of column indices of the nonzeroentries of N where N=(X>2)
c =
1
3
and a logical array that contains thenonzero elements of N where N=(X>2).
v =
1
1
Recall that when you use find on a logicalexpression, the output vector v does not contain the nonzero entries of theinput array. Instead, it contains the nonzero values returned after evaluatingthe logical expression.
Some operations on a vector
x = [11 0 33 0 55]';
find(x)
ans =
1
3
5
find(x == 0)
ans =
2
4
find(0 < x & x < 10*pi)
ans =
1
For the matrix
M = magic(3)
M =
8 1 6
3 5 7
4 9 2
find(M > 3, 4)
returns the indices of the first fourentries of M that are greater than 3.
ans =
1
3
5
6
If X is a vector of all zeros, find(X)returns an empty matrix. For example,
indices = find([0;0;0])
indices =
Empty matrix: 0-by-1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。