How to Iterate through each element in N-Dimensional matrix in MATLAB?


MATLAB is a powerful tool to perform various operations on matrices. In the case of matrices, iterating through elements of a matrix is one of the common operations used to perform various other matrix operations such as finding sum of all elements of a matrix, finding maximum and minimum values in a matrix, and more. In this article, I will explain how to iterate through elements in an N-dimensional matrix in MATLAB.

Iterating Through Elements of a Matrix

In a matrix, iterating through each element means visiting and processing every value present within the matrix.

An N-dimensional matrix is one which has two or more dimensions. For example, if a matrix has rows and columns, then there are two dimensions and hence the matrix is called a 2-dimensional matrix.

Therefore, visiting and processing each value present in rows and columns of a 2D matrix is referred to as iterating through each element in a 2-dimentional matrix. Similarly, we can extend this to N-dimensions.

How to Iterate Through Each Element in N-Dimensional Matrix in MATLAB?

MATLAB provides two efficient methods to iterate through each element in an N-dimensional matrix. They are −

  • Nested Loop Method

  • Vectorized Operation Method

We can use these two methods to iterate through each element of an N-dimensional array in MATLAB.

Let us now understand how to write codes in MATLAB to iterate through each element in a matrix using these two methods.

Iterate Through a Matrix Using Nested Loop Method

The nested loop method is the simplest method that we can use to iterate i.e., visit and process each element in an N-dimensional matrix in MATLAB.

The step-by-step process for iterating through each element in a matrix using the nested loop method is explained below.

  • Step (1) − Input or create the N-dimensional matrix.

  • Step (2) − Determine the size or dimensions of the matrix. This can be done by using the “size()” function.

  • Step (3) − Create a nested loop to iterate through each element of the matrix.

  • Step (4) − Display the result of loop iteration.

Example

Let us now see an example to understand the implementation of these steps in MATLAB.

% Iterate through each element in a matrix using nested loop method
% Create a sample N-dimensional matrix
mat = [2 5 9 4; 3 5 2 7; 8 4 6 1; 4 7 8 6]; 

% Determine the number of rows and columns in the matrix
[row, col] = size(mat);

% Define a nested loop to iterate through each element of the matrix
for i = 1 : row	% Iterate through rows
   for j = 1 : col	% Iterate through columns

      % Obtain the value of the current element
      e = mat(i, j);
        
      % Display the value of the current element
      fprintf('%d ', e);
   end
   fprintf('
'); % line break after printing each row end

Output

When you run this code, it will produce the following output

2   5   9   4   
3   5   2   7   
8   4   6   1   
4   7   8   6 

Code Explanation

In this MATLAB code, we start by creating a sample matrix. Then, we determine the size of the matrix using the “size” function. Next, we define a nested loop to iterate through each element of the matrix, and obtain and display the value of all the elements.

Iterate Through a Matrix Using Vectorized Operations Method

In MATLAB, the vectorized operations method is a more efficient technique to iterate through each element in an N-dimensional matrix than the nested loop method.

It reduces the code length and complexity, making the code simpler. Also, this method is much faster and concise when dealing with large matrices.

The step-by-step process of iterating through each element in an N-dimensional matrix using the vectorized operations method is described below.

  • Step (1) − Create an N-dimensional matrix.

  • Step (2) − Determine the total number of elements in the matrix using the “numel” function.

  • Step (3) − Iterate a simple “for” loop through elements of the matrix to obtain and display their values.

Example

Here is an example in MATLAB showing the implementation of this process.

% Create an example N-dimensional matrix
mat = [2 5 9 4; 3 5 2 7; 8 4 6 1; 4 7 8 6]; 

% Use vectorized operations to iterate through each element of the matrix
elements_num = numel(mat);	% Number of elements in the matrix
for i = 1 : elements_num
   element = mat(i);	% Obtain the element value
    
   % Display the obtained elements
   fprintf('%d ', element);
end

Output

When you run this code, it will produce the following output

2 3 8 4 5 5 4 7 9 2 6 8 4 7 1 6

Code Explanation

In this MATLAB code, we start by creating a matrix. Then, we determine the total number of elements in the matrix using the “numel” function.

After that, we use a “for” loop that iterates through each element of the matrix starting from first element to last element.

Inside this “for” loop, we determine the value of the element at index “i” and store this value in the variable “element”.

After that, we display the obtained values of the elements using the “fprintf” function.

This is how we can iterate through each element in an N-dimensional matrix using the vectorized operations method.

Conclusion

This is all about iterating through each element of an N-dimensional array in MATLAB. It is an important step involved in performing various operations on matrices. In this tutorial, I explained two commonly used methods to iterate through each element in a matrix with the help of examples in MATLAB.

Updated on: 06-Oct-2023

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements