Matlab

How to Find Indices and Values of Nonzero Elements in MATLAB using find() Function

When working with arrays and vectors in MATLAB, it is often necessary to locate and extract the indices and values of non-zero elements. This is a common task in many data processing and analysis applications. Fortunately, MATLAB provides a powerful function called find() that simplifies this process.

In this article, we’ll look at how to use MATLAB’s find() function to locate the indices as well as the values of elements that are not zero.

Understanding the find() Function

Finding the indices as well as the values of non-zero entries within multi-directional arrays or vectors is easy with the help of MATLAB’s find() function. This built-in function is widely used in various applications, allowing for efficient retrieval of specific values from large arrays or vectors. When working with big datasets or having to quickly extract certain numbers, this function is especially helpful.

Syntax
The syntax to use the find() function in MATLAB is given below:

k = find(X)
k = find(X,n)
k = find(X,n,direction)
[row,col] = find(___)
[row,col,v] = find(___)

The above syntax shows that there are five different ways of using the find() function. The functionality of all these methods is described as:

  • k = find(X): Returns a vector that includes the linear indices of each non-zero element in multi-directional array X. The find() function generates a vector having an identical orientation as X if X is a vector. If X represents a multidimensional multi-directional array (N-dimensional array), the find() function returns the column vector with the linear indices of the obtained result.
  • k = find(X,n): Gives the first n indices, which correspond to the nonzero elements in X.
  • k = find(X,n, direction): Identifies the final n indices which relate to nonzero components of X whenever a direction is “last”. “First” is the default direction, which locates the first n indices that correspond to nonzero elements.
  • [row,col] = find(___): This finds the row and column indices of each non-zero element contained in the multi-directional array X using any of the above input arguments.
  • [row,col,v] = find(___): This returns the vector v that contains the non-zero elements contained in the multi-directional array X.

How to Use find() Function in MATLAB

In this section, you will find some basic examples of using the find() function in MATLAB.

Example 1
In this example, we simply declare a vector having zero and non-zero elements. The indices of elements with non-zero values are then determined by using the find() function.

X = [zeros(1,4), (3:3:16)]
k = find(X)

Output

Example 2
In this example, we simply declare a vector having zeros. Then we use find() function, which returns an empty vector because there is no non-zero element.

X = zeros(1,4)
k = find(X)

Output

Example 3
In this example, we simply declare a vector having zero and non-zero elements. Then we use find() function to find the indices of the first three non-zero elements. And print the indices and their corresponding values in the matrix form.

X = [zeros(1,4), (3:3:16)]
k = find(X, 3)
[X(k); k]

Output

Example 4
In this example, we simply declare a vector having zero and non-zero elements. Then we use the find() function to determine the indices of the last three non-zero elements. And print the indices and their corresponding values in the matrix form.

X = [zeros(1,4), (3:3:16)]
k = find(X, 3, 'last')
[X(k); k]

Output

Example 5
In this example, we simply declare a 3-by-3 matrix having zero and non-zero elements. Then we use find() function to determine the indices of non-zero elements.

X = [1 3 2; 0 1 0; 9 0 4];
k=find(X)

Output

Example 6
In this example, we simply declare a 3-by-3 matrix having zero and non-zero elements. Then we use find() function to determine the indices of non-zero entries by storing the index values of non-zero row elements in a vector named row and non-zero column in a vector named col.

X = [1 3 2; 0 1 0; 9 0 4];
[row,col] = find(X)

Output

Example 7
In this example, we simply declare a 3-by-3 matrix having zero and non-zero elements. Then we use find() function to determine the indices of non-zero entries by storing the index values of non-zero row elements in a vector named row and non-zero column in a vector named col. Here, the find() function also determines the non-zero values corresponding to their indices and stores them in a vector named v. After that we create a matrix that has three columns that are row, col, and v respectively.

X = [1 3 2; 0 1 0; 9 0 4]
[row,col,v] = find(X);
[row,col,v]

Output

Conclusion

A built-in find() function in MATLAB is used to determine indices of the non-zero elements in a multi-directional array or a vector. This function accepts a multi-directional array or a vector as an input and returns a vector that contains the indices of non-zero elements. This tutorial explored different ways of finding index values of the non-zero elements using the find() function in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.