Matlab

What Does find() Do in Matlab

One essential function that every MATLAB user should be familiar with is the find() function. The find() function is employed to locate the indices of non-zero or non-empty elements in an array or matrix. In this article, we will explore the different ways the find() function can be utilized in MATLAB, along with relevant examples to illustrate its usage.

What Does find() Do in MATLAB?

The find() function in MATLAB is used to locate the indices of non-zero or non-empty elements in an array or matrix. It gives back a vector with the indices of the components that satisfy the given requirement. The main purpose of the find() function is to identify the positions of elements that satisfy a particular criterion or condition within a given data structure, the basic syntax for the find() function in MATLAB is as follows:

indices = find(array)

Here, the array refers to the input array or matrix, and indices represent the output, which is a vector containing the indices of the elements in the array that are not empty or zero.

1: Finding Non-Zero Elements

The most common usage of the find() function is to locate the indices of non-zero elements in an array, consider the following example:

A = [1 0 2 0 3 0];

indices = find(A);

disp(indices);

In this example, the find() function returns the indices of non-zero elements in the array A, which are 1, 3, and 5:

2: Finding Non-Empty Elements in Cell Arrays

The find() function can also be used to locate the indices of non-empty elements in cell arrays, consider the following example:

C = {[], 'Hello', [], 'Sam'};

indices = find(~cellfun('isempty', C));

disp(indices);

In this case, the find() function is applied to the cell array C after checking if each element is empty using the cellfun function. It returns the indices of the non-empty elements, which are 2 and 4.

3: Finding Elements that Satisfy a Condition

The find() function can be combined with logical expressions to locate elements that satisfy a specific condition, consider the following example:

B = [5 10 15 20 25];

indices = find(B > 15);

disp(indices);

In this example, the find() function is used to identify the indices of elements in array B that are greater than 15. The output provides the indices 4 and 5, corresponding to the values 20 and 25.

A picture containing screenshot, text, line Description automatically generated

4: Finding Specific Elements in Multidimensional Arrays

The find() function can also operate on multidimensional arrays and return indices of specific elements, consider the following example:

M = [1 2 3; 4 5 6; 7 8 9];

indices = find(M == 5);

disp(indices);

Here, the find() function is used to locate the index of the element in the matrix M that is equal to 5, the output reveals that the element is found at index 5.

A picture containing text, software, screenshot Description automatically generated

Conclusion

The find() function in MATLAB is a valuable tool for locating non-zero or non-empty elements in arrays, cell arrays, and multidimensional arrays. By mastering the various ways to use the find() function, MATLAB users can efficiently retrieve indices and extract relevant information from their data. This article has covered some fundamental applications of the find() function with examples.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.