文档帮助中心
本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。
非零矩阵元素的数目
N = nnz(X)
示例
N = nnz(X) 返回矩阵 X 中的非零元素数。
X
全部折叠
创建一个单位矩阵并确定它包含的非零元素数。
X = eye(4)
X = 4×4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
N = 4
将 nnz 与关系运算符结合使用可确定有多少矩阵元素满足条件。由于关系运算符产生由 1 和 0 组成的逻辑矩阵,因此 nnz 函数会对值为 1(即条件为 true)的元素进行计数。
nnz
创建一个矩阵并确定有多少元素大于 10。
X = magic(5)
X = 5×5 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
nnz(X>10)
ans = 15
矩阵的密度是指非零元素数与元素总数之比,即 nnz(X)/numel(X)。
nnz(X)/numel(X)
创建一个表示 L 形域上的有限差分拉普拉斯算子的稀疏矩阵,并计算其密度。
X = delsq(numgrid('L',20)); spy(X)
d = nnz(X)/numel(X)
d = 0.0194
结果表明,矩阵中只有约 2% 的元素是非零元素。
输入矩阵。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | duration | calendarDuration 复数支持: 是
single
double
int8
int16
int32
int64
uint8
uint16
uint32
uint64
logical
duration
calendarDuration
此函数完全支持 tall 数组。有关详细信息,请参阅 tall 数组。
此函数完全支持 GPU 数组。有关详细信息,请参阅Run MATLAB Functions on a GPU (Parallel Computing Toolbox)。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
find | nonzeros | numel | nzmax | size
find
nonzeros
numel
nzmax
size
您的系统上存在此示例的修改版本。是否要打开此版本?
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office