# The nnz() Function in MATLAB

This tutorial will discuss finding the number of nonzero elements present in a vector or matrix using the `nnz()` function in MATLAB.

## Use the `nnz()` Function in MATLAB

The `nnz()` function of MATLAB is used to find the nonzero elements present in a vector or matrix. The following is the basic syntax of the `nnz()` function.

Syntax:

``````num = nnz(m)
``````

The syntax will return the total number of nonzero elements stored inside the matrix or vector `m` and store it in the variable `num`. The input of the `nnz()` function should be a matrix or vector.

For example, let’s create a vector and find the number of nonzero elements present using the `nnz()` function.

Code:

``````m = [0 1 0 5]
num = nnz(m)
``````

Output:

``````m =

0   1   0   5

num = 2
``````

We used a vector containing four-element in the above code, and two of these elements are zero. We can see that the `nnz()` function returned two, which means two nonzero elements are present in the given matrix or vector.

We can also define conditions inside the `nnz()` function. If we only want to find the number of elements greater than 1, we can use the greater than symbol to define the condition inside the `nnz()` function.

For example, let’s find the number of elements greater than 1 present in the above matrix.

Code:

``````m = [0 1 0 5]
num = nnz(m>1)
``````

Output:

``````m =

0   1   0   5

num = 1
``````

In the above example, we’ve defined a condition inside the `nnz()` function to return the number of elements greater than 1. The number of elements greater than one is 1, as we can see in the output.

If we don’t define a condition, the `nnz()` function will return the number of elements greater than 0, but we can change that condition. We can also set the `nnz()` function to return the number of elements greater than some negative number like -5, and in this case, the function will count all the elements greater than -5, including zero.

For example, let’s count the number of elements greater than a negative value present in a matrix.

Code:

``````m = [-5 -1 1 0 5]
num = nnz(m>-5)
``````

Output:

``````m =

-5  -1   1   0   5

num = 4
``````

As we can see, four numbers are greater than `-5` in the above matrix, and the `nnz()` function also returned four as output. We can use the greater than `>`, less than `<`, equal to `=`, greater than or equal to `>=`, less than or equal to `<=`, logical AND `&`, and logical OR `|` operators to define our condition inside the `nnz()` function.

The AND `&` and OR `|` operators are useful if we want to define more than one condition. In the case of AND operator, the element will be counted if it satisfies all the conditions, and in the case of the OR operator, the element will be counted if it satisfies even one condition.

If we want to count the number of elements greater than a certain value and the elements that are less than a certain value, we can use the AND operator to define two conditions. If we want to count the number of elements greater than a certain value and the elements greater than another value, we can use the OR operator.

For example, let’s count the number of elements present in a matrix using multiple conditions inside the `nnz()` function.

Code:

``````m = [15 10 1 0 5]
num = nnz(m>1 & m<15)
num_2 = nnz(m>5 | m<1)
``````

Output:

``````m =

15   10    1    0    5

num = 2
num_2 = 3
``````

We used the AND operator to find the number of elements greater than 1 and less than 15, and we know that there are only two elements that are also returned by the `nnz()` function. We also used the OR operator to find the number of elements greater than 5 or less than 1, and we know that two elements are greater than 5 and one elements is less than 1.

The total number of elements will be 3, which is returned by the `nnz()` function. We can also use both the AND and OR operator inside the `nnz()` function to define a condition and define any number of conditions.

Check this documentation for more details about the `nnz()` function.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article - MATLAB Matrix

• MATLAB Sort Rows
• MATLAB Norm() Function
• Append Vector to 3D Matrix in MATLAB
• MATLAB Repmat