How to Create a Matrix from a Nested Loop in MATLAB?


MATLAB is an efficient tool to create matrices. In MATLAB, a matrix is a two-dimensional array that can store data in the form of rows and columns. MATLAB allows us to create matrices by using nested loops. In this tutorial, we will learn how to create a matrix using a nested loop in MATLAB.

In MATLAB, creating a matrix using nested loop is a straightforward two step process. The step-by-step process to create a matrix using a nested loop is explained below −

  • Step (1) − Initialize the matrix and specify the loop limits depending on the number of rows and columns in the matrix.

  • Step (2) − Create a nested loop, either 'for' loop or 'while' loop. Utilize the variables of the loop to index the columns and rows of the matrix, and set the values of matrix elements accordingly.

Example (1) – Create a 3 × 3 Matrix With Elements Increasing From 1 to 9

Now, let us practically understand the process of creating a matrix using nested loop in MATLAB with the help of examples.

% MATLAB program to create a matrix using nested loop
% Initialize a matrix
m = [];

% Initialize a variable to keep track of the value
x = 1;

% Create a nested loop and specify loop limits
for i = 1 : 3    % Outer loop for rows
   for j = 1 : 3    % Inner loop for columns
      m(i, j) = x;
      x = x + 1;
   end 
end

% Display the matrix
disp('The output matrix is:')
disp(m);

Output

The output matrix is:
     1     2     3
     4     5     6
     7     8     9

Code Explanation

In this MATLAB code, we start by initializing an empty matrix and store in a variable 'm'. Next, we initialize a variable 'x = 1' to keep track the value of each matrix element. After that we create two nested loops by using 'for' loop. The outer nested loop i.e., 'for i = 1 : 3' is the loop for rows, and the inner loop i.e. 'for j = 1 : 3' is the loop for columns.

Then, inside the nested loops, we assign the value of the variable 'x' to element of the 'ith' row and 'jth' column as 'm(i, j) = x'. Next, we write a formula 'x = x + 1', which increments the value of 'x' when go from one element to the next.

This loop will continuously iterate until all the elements of the matrix are filled with their respective values.

Finally, we use the 'disp' function to display the resulting matrix. The output of this code will be a 3 × 3 matrix with elements increasing from 1 to 9.

Example (2) – Create a 4 × 4 Diagonal Matrix

% MATLAB program to create a 4 × 4 diagonal matrix using nested loop
% Initialize a matrix with all zero elements
m = zeros(4, 4);

% Create a nested loop to set the diagonal elements of matrix to 1
for i = 1 : 4
    m(i, i) = 1;
end

% Display the resulting matrix
disp('The 4 × 4 diagonal matrix is:')
disp(m);

Output

The 4 × 4 diagonal matrix is:
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

Code Explanation

In this MATLAB code, firstly we initialize a matrix with all zero elements and store it in a variable 'm'. Then, we create a nested 'for' loop to set all the diagonal elements of the matrix to 1. For this, we assign '1' to each element with same row and column index i.e., 'm(i, i) = 1'. Finally, we display the resulting matrix using the 'disp' function. The output of this MATLAB code will a 4 x 4 diagonal matrix.

Example (3) – Create a 3 x 3 Matrix Where Each Element Represents the Sin Value of the Row Indices

% MATLAB program to create a 3 x 3 matrix with sin values of row indices
% Initialize a matrix
m = [];

% Create nested loops to compute the sin values for row indices
for i = 1 : 3	% Outer loop for rows
   for j = 1 : 3	% Inner loop for columns
       m(i, j) = sin(i);
   end 
end

% Display the resulting matrix
disp('The 3x3 matrix with sine values of row indices:')
disp(m);

Output

The 3x3 matrix with sine values of row indices:
    0.8415    0.8415    0.8415
    0.9093    0.9093    0.9093
    0.1411    0.1411    0.1411

Code Explanation

In this MATLAB code, firstly we create a 3 x 3 matrix 'm' with all zero elements. Then, we create nested loops to calculate the sin value for each matrix element. Here, the outer loop 'i = 1 : 3' is for rows and the inner loop 'j = 1 : 3' is for columns.

Next, inside the loops, we calculate the sine value of each row index 'sin(i)' and assign this value to each element at 'ith' row and 'jth' column of the matrix.

Finally, we display the resulting 3 x 3 matrix using the 'disp' function in which each element represents the sine value of its corresponding row index.

Conclusion

This is all about creating a matrix from nested loops in MATLAB. In MATLAB programming, we can easily create any kind of matrix just by iterating a nested loop to fill the elements of the matrix with a certain value required. The above example programs demonstrate the implementation of nested loops to create different kinds of matrices.

Updated on: 06-Sep-2023

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements