查找非零元素的索引和值
全页折叠
语法
k = find(X)
k = find(X,n)
k = find(X,n,direction)
[row,col] = find(___)
[row,col,v] = find(___)
说明
示例
k = find(X)
返回一个包含数组 X
中每个非零元素的线性索引的向量。
如果
X
为向量,则find
返回方向与X
相同的向量。如果
X
为多维数组,则find
返回由结果的线性索引组成的列向量。
示例
k = find(X,n)
返回与 X
中的非零元素对应的前 n
个索引。
示例
k = find(X,n,direction)
(其中 direction
为 'last'
)查找与 X
中的非零元素对应的最后 n
个索引。direction
的默认值为 'first'
,即查找与非零元素对应的前 n
个索引。
示例
[row,col] = find(___)
使用上述语法中的任何输入参量返回数组 X
中每个非零元素的行和列下标。
示例
[row,col,v] = find(___)
还返回包含 X
的非零元素的向量 v
。
示例
全部折叠
矩阵中的零和非零元素
打开实时脚本
在 3×3 矩阵中查找非零元素。
X = [1 0 2; 0 1 1; 0 0 4]
X = 3×3 1 0 2 0 1 1 0 0 4
k = find(X)
k = 5×1 1 5 7 8 9
对 X
使用逻辑 not
运算符以查找零值。
k2 = find(~X)
k2 = 4×1 2 3 4 6
满足一个条件的元素
打开实时脚本
在 4×4 幻方矩阵中查找前五个小于 10 的元素。
X = magic(4)
X = 4×4 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
k = find(X<10,5)
k = 5×1 2 3 4 5 7
查看 X
的对应元素。
X(k)
ans = 5×1 5 9 4 2 7
等于特定值的元素
打开实时脚本
要查找特定的整数值,请使用 ==
运算符。例如,在 1×10 的奇数向量中查找等于 13
的元素。
x = 1:2:20
x = 1×10 1 3 5 7 9 11 13 15 17 19
k = find(x==13)
k = 7
要查找非整数值,请基于您的数据使用容差值。否则,由于浮点舍入误差有时会生成空矩阵。
y = 0:0.1:1
y = 1×11 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
k = find(y==0.3)
k = 1x0 empty double row vector
k = find(abs(y-0.3) < 0.001)
k = 4
最后几个非零元素
打开实时脚本
创建一个由等于零的所有奇数索引元素组成的 6×6 幻方矩阵。
X = magic(6);X(1:2:end) = 0
X = 6×6 0 0 0 0 0 0 3 32 7 21 23 25 0 0 0 0 0 0 8 28 33 17 10 15 0 0 0 0 0 0 4 36 29 13 18 11
查找后四个非零值。
k = find(X,4,'last')
k = 4×1 30 32 34 36
满足多个条件的元素
打开实时脚本
在 4×4 矩阵中查找前 3 个大于 0
且小于 10
的元素。指定两个输出以便将行和列下标返回到这些元素。
X = [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
X = 4×4 18 3 1 11 8 10 11 3 9 14 6 1 4 3 15 21
[row,col] = find(X>0 & X<10,3)
row = 3×1 2 3 4
col = 3×1 1 1 1
第一个实例是 X(2,1)
,即 8
。
非零元素的下标和值
打开实时脚本
在 3×3 矩阵中查找非零元素。指定三个输出以返回行下标、列下标和元素值。
X = [3 2 0; -5 0 7; 0 0 1]
X = 3×3 3 2 0 -5 0 7 0 0 1
[row,col,v] = find(X)
row = 5×1 1 2 1 2 3
col = 5×1 1 1 2 3 3
v = 5×1 3 -5 2 7 1
多维数组的下标
打开实时脚本
在 4×2×3 数组中查找非零元素。指定两个输出 row
和 col
以返回非零元素的行和列下标。如果输入是多维数组 (N > 2
),find
返回 col
作为 X
的 N-1
个尾部维度的线性索引。
X = zeros(4,2,3);X([1 12 19 21]) = 1
X = X(:,:,1) = 1 0 0 0 0 0 0 0X(:,:,2) = 0 0 0 0 0 0 1 0X(:,:,3) = 0 1 0 0 1 0 0 0
[row,col] = find(X)
row = 4×1 1 4 3 1
col = 4×1 1 3 5 6
输入参数
全部折叠
X
— 输入数组
标量 | 向量 | 矩阵 | 多维数组
输入数组,指定为标量、向量、矩阵或多维数组。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
复数支持: 是
n
— 要查找的非零元素数量
正整数标量
要查找的非零元素数量,指定为正整数标量。默认情况下,find(X,n)
查找 X
中的前 n
个非零元素。
direction
— 搜索方向
'first'
(默认) | 'last'
搜索方向,指定为字符串 'first'
或 'last'
。使用 find(X,n,'last')
在 X
中查找后 n
个非零元素。
输出参量
全部折叠
k
— 指向非零元素的索引
向量
指向非零元素的索引,以向量的形式返回。
如果
X
是行向量,则k
也是行向量。否则,k
为列向量。当
X
是空数组或没有非零元素时,k
是空行向量或空列向量。find
使用如下约定:当X
为空矩阵[]
时,k
也为空矩阵[]
。
可以使用 X(k)
返回 X
中的非零值。
row
— 行下标
向量
行下标,以向量的形式返回。row
和 col
一起指定与 X
中的非零元素对应的 X(row,col)
下标。
col
— 列下标
向量
列下标,以向量的形式返回。row
和 col
一起指定与 X
中的非零元素对应的 X(row,col)
下标。
如果 X
是多维数组且 N > 2
,则 col
是 X
的 N-1
尾部维度的线性索引。这会保留关系 X(row(i),col(i)) == v(i)
。
v
— X
的非零元素
向量
X
的非零元素,以向量的形式返回。
详细信息
全部折叠
线性索引
线性索引允许使用单个下标创建指向数组的索引,例如A(k)
。MATLAB® 将该数组视为单列向量,其中每一列附加到前一列的底部。因此,线性索引从上到下、从左到右对列中的元素编号。
例如,假设一个 3×3 矩阵。您可以用 A(5)
来引用 A(2,2)
元素,用 A(8)
来引用 A(2,3)
元素。线性索引根据数组大小而改变;A(5)
会为 3×3 矩阵和 4×4 矩阵返回不同位置上的元素。
sub2ind
和 ind2sub
函数对于在下标和线性索引之间进行转换非常有用。
提示
要查找符合条件的数组元素,请结合使用
find
和关系表达式。例如,find(X<5)
返回X
中小于5
的元素的线性索引。要直接查找
X
中满足条件X<5
的元素,请使用X(X<5)
。尽量避免使用X(find(X<5))
之类的函数调用,因为这种调用中对逻辑矩阵使用的find
完全没有必要。将
find
与X>1
之类的关系运算结合在一起执行时,必须记住关系运算的结果是由 1 和 0 组成的逻辑矩阵。例如,命令[row,col,v] = find(X>1)
会返回由v
的逻辑值1
(true
) 组成的列向量。行下标和列下标,即
row
和col
与k
×k = sub2ind(size(X),row,col)
中的线性索引相关。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
用法说明和限制:
X
必须是 tall 列向量。
有关详细信息,请参阅 tall 数组。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
如果可变大小输入在运行时变为行向量,代码生成将结束并报告错误。当输入为标量或可变长度行向量时,此限制不适用。
对于可变大小输入,空输出(0×0、0×1 或 1×0)的形状取决于输入大小的上限。当输入数组在运行时为标量或 [] 时,输出可能与 MATLAB 不匹配。如果输入为可变长度行向量,则空输出的大小为 1×0。否则,大小为 0×1。
生成的代码始终返回可变长度向量。即使您提供输出向量
k
,输出也不是固定大小,因为输出可以包含少于k
个元素。例如,find(x,1)
返回具有一个或零个元素的可变长度向量。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
GPU 数组
通过使用 Parallel Computing Toolbox™ 在图形处理单元 (GPU) 上运行来加快代码执行。
此函数完全支持 GPU 数组。有关详细信息,请参阅Run MATLAB Functions on a GPU (Parallel Computing Toolbox)。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2006a 之前推出
另请参阅
ind2sub | nonzeros | strfind | sub2ind | Short-Circuit AND | Short-Circuit OR | ismember
主题
- 查找符合条件的数组元素
- 数组索引
- 关系运算
- 稀疏矩阵
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office