| by Arround The Web | No comments

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

Matrices are essential data structures in MATLAB that can efficiently store and manipulate multidimensional data. Using matrices, we can generate plots and design algorithms. In MATLAB programming there are multiple ways of creating matrices, however, this article covers matrix designing using a Nested Loop.

Creating a Matrix from a Nested Loop

A nested loop is characterized by one loop being contained inside another loop. In MATLAB, nested loops are commonly used to generate matrices of various sizes.

To construct a matrix using a nested loop, we must establish two loops: an outer loop and an inner loop. The outer loop is responsible for iterating through matrix rows, while the inner loop iterates through the columns of the matrix.

Following is the nested loop syntax used for creating new matrices:

for i = 1:numRows
  for j = 1:numColumns
    matrix(i, j) = someExpression;
  end
end

 
Here we have defined rows and columns using the names numRows and numColumns respectively. someExpression is an expression that will be evaluated to get the value for each element in the matrix.

Example 1: Creating a Matrix from a Nested Loop

The given code creates a matrix using a nested loop:

numRows = 3;
numColumns = 3;

matrix = zeros(numRows, numColumns);

for i = 1:numRows
  for j = 1:numColumns
    matrix(i, j) = i * j;
  end
end

disp(matrix)

 
This code will create a 3×3 matrix and display it on the screen. The output will be:

Example 2: Creating a Matrix with Incremental Values

This example creates a matrix with incremental values by using nested loops to iterate over each element of the matrix and assign a unique value to it.

rows = 3;
cols = 4;
matrix = zeros(rows, cols);
value = 1;

for i = 1:rows
    for j = 1:cols
        matrix(i, j) = value;
        value = value + 1;
    end
end

disp(matrix);

 

Example 3: Creating a Matrix with Random Values

The second example generates a matrix with random values by utilizing nested loops to iterate over each element and assigning a random value between 1 and 10 to it using the randi() function.

rows = 4;
cols = 5;
matrix = zeros(rows, cols);

for i = 1:rows
    for j = 1:cols
        matrix(i, j) = randi([1, 10]);
    end
end

disp(matrix);

 

Example 4: Creating a Matrix with User Input Values

This example allows the user to input values for the matrix by utilizing nested loops to iterate over each element and prompt the user for input, which is then assigned to the corresponding element of the matrix using the input function.

rows = 2;
cols = 3;
matrix = zeros(rows, cols);

for i = 1:rows
    for j = 1:cols
        matrix(i, j) = input(sprintf('Enter value for (%d,%d): ', i, j));
    end
end

disp(matrix);

 

Conclusion

This article explored the process of generating a matrix in MATLAB using a nested loop. A nested loop refers to a loop that is enclosed within another loop. MATLAB allows the utilization of nested loops to create matrices of varying sizes. We learned that nested loops can be used to create matrices of any size. Read more on creating a matrix in MATLAB using nested loops in this article.

Share Button

Source: linuxhint.com

Leave a Reply