How to Find Indices and Values of Nonzero Elements in MATLAB?


MATLAB is a powerful tool to perform operations and calculations related to arrays and matrices. In MATLAB, an array or a matrix is a type of data structure used to store numeric data. Each value stored in an array or matrix is termed as an element. The position of an element within an array or matrix is referred to as the index of the element.

MATLAB provides a built−in function "find" which enable us to find the indices and values of non−zero elements in a given array or matrix. However, in MATLAB, we can also find the values and indices of non-zero element using loop mechanism i.e., without using the "find" function.

Both these methods of finding the values and indices of non−zero elements in an array using MATLAB are explained below.

(1). Find Indices and Vaues of Non−Zero Elements without using "find" Function

In MATLAB, we can find the indices and values of non−zero elements in an array or matrix without using the "find" function. To do this, we iterate a loop through the elements of the matrix to check whether they are zero or not. If the element is a non-zero value, the loop will extract its index and value and store in another arrays.

Example

Let us take an example to practically understand the process of finding indices and values of non−zero elements without using the "find" function.

% MATLAB program to find indices and values of non-zero elements without using "find" function
% Create an example matrix
X = [0, 5, 0, 3; 0, 2, 1, 0; 2, 0, 7, 0; 1, 0, 3, 7];

% Create an empty array to store indices of non-zero elements 
I = [];

% Create an empty array to store values of non-zero elements 
V = [];

% Determine the size of the input matrix
[m, n] = size(X);

% Iterate a loop through elements of the matrix
for i = 1 : m
   for j = 1 : n
      if X(i, j) ~= 0	% Check if the element is non-zero
         % Store the indices of non-zero elements
         I = [I; i, j];
         % Store the values of non-zero elements
         V = [V; X(i, j)];
      end
   end
end

% Display the results
disp('Input matrix:');
disp(X);

disp('Indices of non-zero elements:');
disp(I);

disp('Values of non-zero elements:');
disp(V);

Output

Input matrix:
     0     5     0     3
     0     2     1     0
     2     0     7     0
     1     0     3     7

Indices of non-zero elements:
     1     2
     1     4
     2     2
     2     3
     3     1
     3     3
     4     1
     4     3
     4     4

Values of non-zero elements:
     5
     3
     2
     1
     2
     7
     1
     3
     7

Code Explanation:

In this MATLAB code, we start by creating a sample matrix "X". Then, we create two empty arrays, one to store indices of non-zero elements "I" and another is to store values of non−zero elements "V".

After that we determine the size of the matrix or array to define an appropriate loop. Then, we specify the "for" loop that finds the non−zero elements in the input matrix and store their indices and values in the arrays "I" and "V" respectively.

Finally, we display the input matrix, the indices of non-zero elements and their values.

(2). Find Indices and Vaues of Non-Zero Elements using "find" Function

In MATLAB, the "find" function is a built-in function that is used to find the indices and values of non-zero elements in an array or matrix.

Syntax

To find the indices and values of non-zero element, we can use the following syntax of the "find" function,

[row_ind, col_ind, V] = find(X);

Here, the variable "row_ind" stores the row indices of non-zero elements, the variable "col_ind" stores the column index of the non−zero elements, "V" stores the values of non-zero elements, and X is the input matrix.

Let us see an example to understand how to implement MATLAB code to find the indices and values of non−zero elements in an array using the "find" function.

Example

% MATLAB code to find indices and values of non-zero elements using "find" function
% Create an input matrix
X = [0, 5, 0, 3; 0, 2, 1, 0; 2, 0, 7, 0; 1, 0, 3, 7];

% Find indices and values of nonzero elements
[row_ind, col_ind, V] = find(X);

% Display the results
disp('Input matrix:');
disp(X);

disp('Row indices of non-zero elements:');
disp(row_ind);

disp('Column indices of non-zero elements:');
disp(col_ind);

disp('Values of non-zero elements:');
disp(V);

Output

Input matrix:
     0     5     0     3
     0     2     1     0
     2     0     7     0
     1     0     3     7

Row indices of non-zero elements:
     3
     4
     1
     2
     2
     3
     4
     1
     4

Column indices of non-zero elements:
     1
     1
     2
     2
     3
     3
     3
     4
     4

Values of non-zero elements:
     2
     1
     5
     2
     1
     7
     3
     3
     7

Code Explanation

In this MATLAB example, firstly, we create an input matrix "X" containing zero and non-zero elements. Then, we call the "find" function to determine indices and values of non-zero elements. The indices of non−zero elements are stored in vectors "row_ind" and "col_ind", and the values are stored in the vector "V".

Finally, we display the input matrix, row indices and column indices of non-zero elements, and the values of non−zero elements.

Conclusion

In this tutorial, I have explained two different methods of finding indices and values of non−zero elements in an array or matrix using MATLAB. In the first method, we have used a "for" loop to find the non−zero elements in the array and then their indices and values. In the second method, we have used a built−in function "find" to determine the indices and values of non−zero elements.

Updated on: 10-Oct-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements