nnz
Number of nonzero matrix elements
Syntax
Description
Examples
Number of Nonzeros
Create an identity matrix and determine the number of nonzeros it contains.
X = eye(4)
X = 4×4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
N = nnz(X)
N = 4
Number of Elements Meeting a Condition
Use nnz
in conjunction with a relational operator to determine how many matrix elements meet a condition. Since relational operators produce logical matrices of 1s and 0s, the nnz
function counts the 1s where the condition is true.
Create a matrix and determine how many elements are greater than 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
Density of Sparse Matrix
The density of a matrix is the ratio of nonzeros to the total number of elements, nnz(X)/numel(X)
.
Create a sparse matrix representing the finite difference Laplacian on an L-shaped domain and calculate its density.
X = delsq(numgrid('L',20));
spy(X)
d = nnz(X)/numel(X)
d = 0.0194
The result indicates that only about 2% of the elements in the matrix are nonzero.
Input Arguments
X
— Input matrix
matrix
Input matrix.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| duration
| calendarDuration
Complex Number Support: Yes
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
nnz
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The nnz
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
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
- United Kingdom (English)