How to Find Indices and Values of Nonzero Elements in MATLAB?
Last Updated :
28 Apr, 2025
MATLAB provides functionality that finds the indices and values of all non-zero elements in a vector or multidimensional array; the find() function. The find function with required parameters gives back a vector containing indices of non-zero elements in the passed array/vector.
Syntax:
vec = [] %some vector or array
indices = find(vec,...);
Here, the vec could be a vector or array. The indices vector stores the indices of non-zero elements in vec. Now, let us see different forms of find with the help of examples.
Finding non-zero elements' indices in a vector.
Example 1:
Matlab
% MATLAB code for indices
% Vector
vec = [1 0 2 2 4 3 5 0 76 23 0.1 -1 0];
% Getting indices
indices = find(vec);
% Displaying the indices
disp(indices)
Output:
When an array is passed to the find() function, it returns a column vector that stores the linear indices of non-zero elements i.e., indexing when counting each element column-wise.
Example 2:
Matlab
% MATLAB code for vector
% 2D array
vec = [1 0 2 2 4;
3 5 0 76 23;
0.1 -1 0 23 0];
% Getting indices
indices = find(vec);
% Displaying indices
disp(indices)
Output:
We can find the indices of n non-zero elements from starting from the beginning or end as well.
Example 3:
Matlab
% MATLAB code for indices with non-zero elements
vec = [1 0 2 2 4;
3 5 0 76 23;
0.1 -1 0 23 0];
% Getting first 7 non-zero elements' indices
indices_first = find(vec,7,"first");
% Getting last 7 non-zero elements' indices
indices_last = find(vec,7,"last");
Output:
Note: In case "first" or "last" is not specified, MATLAB takes "first" as a default setting.
Getting Values of Non-Zero Elements With Indices:
We can also get the values of non-zero elements along with their indices.
Example 4:
Matlab
% MATLAB code
% Array
vec = [1 0 2 2 4;
3 5 0 76 23;
0.1 -1 0 23 0];
% Getting row, column value of non
% zero element with their values
[row_ind, col_ind, values] = find(vec,7,"first");
Output:
This will create three vectors:
- row_ind will store the row number of non-zero elements.
- col_ind will store the column number of non-zero elements.
- values store the values of non-zero elements.
The same can be done with vectors. In that case, one row_ind or col_ind will remain a constant vector.
Example 5:
Matlab
% MATLAB code for vector
vec = [1 0 2 2 4 3 5 0 76 23 0.1 -1 0 23 0];
% Getting the values of row, col,
% and values of non-zero element
[row_ind, col_ind, values] = find(vec,7,"first");
Output:
Similar Reads
How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. Â In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi
4 min read
How to find sum of elements of an array in MATLAB? This article will discuss the "Finding sum of elements of an array" in MATLAB that can be done using multiple approaches which are illustrated below. Â Using sum(A)Â This is used to return the sum of the elements of the array along the first array dimension whose size does not equal 1. It returns a ro
4 min read
How to find missing values in a matrix in R In this article, we will examine various methods for finding missing values in a matrix by using R Programming Language. What are missing values?The data points in a dataset that are missing for a particular variable are known as missing values. These missing values are represented in various ways s
3 min read
Find Indices of Maximum and Minimum Value of Matrix in MATLAB Matrices in MATLAB are 2-dimensional arrays that store mostly numeric data at different indices. Now, to find the indices of maximum and minimum values of a given matrix, MATLAB does not provide any direct functionality however, we can do the same by using two other functionalities. Firstly, we will
3 min read
How to Find the Position of a Number in an Array in MATLAB? Finding the position of a number in an array, which can be done using the find() function. The find() function is used to find the indices and values of the specified nonzero elements. Syntaxfind(X) Parameters: This function accepts a parameter. X: This is the specified number whose position is goin
1 min read
How to detect duplicate values and its indices within an array in MATLAB? In this article, we will discuss how to find duplicate values and their indices within an array in MATLAB. It can be done using unique(), length(), setdiff(), and numel() functions that are illustrated below: Using Unique() Unique(A) function is used to return the same data as in the specified array
3 min read