matlab中find()函数用法
一.基本用法返回矩阵或向量中非零元素的索引注意:matlab中下标从1开始举例:(1)向量返回非零元素下标find(vector)x=[12 3 0 0 67 8 9];find(x)ans =1236789返回前k个非零元素的下标find(vector,k)或find(vector,k,‘first’)x=[12 3 0 0 67 8 9];find(x,2)ans
·
一.基本用法
返回矩阵或向量中非零元素的索引
注意:matlab中下标从1开始
举例:
(1)向量
返回非零元素下标
find(vector)
x=[1 2 3 0 0 6 7 8 9];
find(x)
ans =
1 2 3 6 7 8 9
返回前k个非零元素的下标
find(vector,k)
或find(vector,k,‘first’)
x=[1 2 3 0 0 6 7 8 9];
find(x,2)
ans =
1 2
find(x,2,'first')
ans =
1 2
返回后k个非零元素的下标
x=[1 2 3 0 0 6 7 8 9];
find(x,2,'last')
ans =
8 9
(2)矩阵
a.返回下标
find(matrix)
x=[1 2 3;
0 0 4;
7 9 0];
>> find(x)
ans =
1
3
4
6
7
8
注意:matlab中存储矩阵是按列存储的
举例:
x(3)
ans =
7
b.返回行号与列号
[r,c]=find(matrix)
>> x=[1 2 3;
0 0 4;
7 9 0];
[r,c]=find(x);
>> [ r,c]
ans =
1 1
3 1
1 2
3 2
1 3
2 3
c.返回行号,列号,取值
[r,c,v]=find(matrix)
>> x=[1 2 3;
0 0 4;
7 9 0];
>> [r,c,v]=find(x);
>> [r,c,v]
ans =
1 1 1
3 1 7
1 2 2
3 2 9
1 3 3
2 3 4
二.进阶用法
find()函数的功能是找到向量或者矩阵中不为0的元素,还可以找到满足某些条件的元素
举例:
(1)
>> x=[1 2 3 4 5 6 7]
x =
1 2 3 4 5 6 7
>> find(x>=5)
ans =
5 6 7
(2)
>> x=[1 2 3 4 5 6];
>> find(x==4)
ans =
4
(3)
>> x=[1 2 3 4 5 6 7 8 9];
>> find(x>3&x<8)
ans =
4 5 6 7
(4)
判断向量中是否含有某个元素
>> x=[1 2 3 4 5 6 7 8 9];
>> ~isempty(find(3))
ans =
logical
1
isempty(A)
A为空,返回1
A非空,返回0
更多推荐
所有评论(0)