unique函数
格式 b = unique (a) %取集合a的不重复元素构成的向量
b = unique (A,'rows') %返回A、B不同行元素组成的矩阵
[b,i,j] = unique (…) %i体现b中元素在原向量(矩阵a)中的位置;j体现原向量(矩阵a)在b中的位置
例1-39
>> A=[1 1 2 2 4 4 6 4 6]
A =
1 1 2 2 4 4 6 4 6
>> [c,i,j]=unique(A)
c =
1 2 4 6
i =
2 4 8 9 %i体现b中元素在原向量(矩阵a)中的位置;
j =
1 1 2 2 3 3 4 3 4 %j体现原向量(矩阵a)在b中的位置
例1-40
>> A=[1 2 2 4;1 1 4 6;1 1 4 6]
A =
1 2 2 4
1 1 4 6
1 1 4 6
>> [c,i,j]=unique(A,'rows')
c =
1 1 4 6
1 2 2 4
i =
3
1
j =
2
1
1
find -Find indices and values of nonzero elements
Syntax
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, ...)
Description
ind = find(X) locates all nonzero elements of array X, and returns the linear indices of those 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 an empty array, then ind is an empty array.
ind = find(X, k) or ind = find(X, k, 'first') returns at most the first k indices corresponding to the nonzero entries of X. k must be a positive integer, but it can be of any numeric data type.
ind = find(X, k, 'last') returns at most the last k indices corresponding to the nonzero entries of X.
[row,col] = find(X, ...) returns the row and column indices of the nonzero entries in the matrix X. This syntax is especially useful when working with sparse matrices. If X is an N-dimensional array 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 atX(4,2,3), find returns 4 in row and 16 in col. That is, (7 columns in page 1) + (7 columns in page 2) + (2 columns in page 3) = 16.
[row,col,v] = find(X, ...) returns a column or row vector v of the nonzero entries in X, as well as row and column indices. If X is a logical expression, then v is a logical array. Output v contains the non-zero elements of the logical array obtained 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 logical array that contains the nonzero elements of N where
N=(A>10)
Examples
Example 1
X = [1 0 4 -3 0 0 0 8 6]; indices = find(X)
returns linear indices for the nonzero entries of X.
indices = 1 3 4 8 9
Example 2
You can use a logical expression to define X. For example,
find(X > 2)
returns linear indices corresponding to the entries of X that are greater than 2.
ans = 3 8 9
Example 3
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 the nonzero entries of X
r = 1 2 1 2 3
a vector of column indices of the nonzero entries of X
c = 1 1 2 3 3
and a vector containing the nonzero entries of X.
v = 3 -5 2 7 1
Example 4
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 the nonzero entries of N where N=(X>2)
r = 1 2
a vector of column indices of the nonzero entries of N where N=(X>2)
c = 1 3
and a logical array that contains the nonzero elements of N where N=(X>2).
v = 1 1
Recall that when you use find on a logical expression, the output vector v does not contain the nonzero entries of the input array. Instead, it contains the nonzero values returned after evaluating the logical expression.
Example 5
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
Example 6
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 four entries of M that are greater than 3.
ans = 1 3 5 6
Example 7
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
isnan函数
[功能描述]
判断数组中的元素是否为无穷大 [函数描述] tf=isnan(A):返回一个与A相同维数的数组,若A的元素为NaN(非数值),在对应位置上返回逻辑1(真),否则返回逻辑0(假)。对虚数z,如果z的实部或虚部都是NaN,那么isnan函数返回逻辑1,如果实部和虚部都是inf,则返回逻辑0。
eye函数
Y = eye(n) 返回n乘n单一矩阵 Y = eye(m,n) or eye([m n]) 对角线是1,其他是0。 Y = eye(size(A)) 返回统一大小的单一矩阵
堆叠矩阵repmat
使用用法如下:
B = repmat(A,m,n)
B = repmat(A,[m n])
B = repmat(A,[m n p...])
这是一个处理大矩阵且内容有重复时使用,其功能是以A的内容堆叠在(MxN)的矩阵B中,B矩阵的大小由MxN及A矩阵的内容决定,如果A是一个3x4x5的矩阵,有B = repmat(A,2,3)则最后的矩阵是6x12x5
例如:
>>B=repmat( [1 2;3 4],2,3)
B =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
其结果变为4X6。A也可以置放文字串,如:
>>C=repmat(' Long live the king!', 2,2)
C =
Long live the king! Long live the king!
Long live the king! Long live the king!
也可置放其他的:
>> D=repmat(NaN,2,5)
D =
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN