How to Create a Matrix From a Nested Loop in MATLAB? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Matrices are 2-dimensional arrays that store numeric or symbolic data. It is convenient to create them with the help of nested loops as the outer loop creates the elements along one dimension and the inner loop creates the elements along the second dimension. In this article, we will see how to create matrices from nested loops with various examples. Create a matrix that has elements representing the sum of the row and column numbers where the indexing starts from 1. Example 1: Matlab % Matlab code for Nested loop % Empty matrix mat = []; %nested loop for i = 1:5 %outer loop for rows for j = 1:5 %inner loop for columns mat(i,j) = i+j; end end %printing matrix disp(mat) Output: Resultant matrix: Create a matrix where each element represents (row-number % column-number). Example 2: Matlab % MATLAB code for Nested for loop % empty matrix mat = []; % Nested loop for i = 1:5 %outer loop for rows for j = 1:5 %inner loop for columns mat(i,j) = mod(i,j); end end % Printing matrix disp(mat) Output: Resultant matrix: Create a matrix where each element represents the exponential sum of the row and column numbers. Example 3: Matlab % MATLAB code % empty matrix mat = []; %nested loop for i = 1:3 %outer loop for rows for j = 1:3 %inner loop for columns mat(i,j) = exp(i+j); end end %printing matrix disp(mat) Output: Resultant matrix: Comment More infoAdvertise with us Next Article How to Create a Nested For Loop in R? O owl0223 Follow Improve Article Tags : Software Engineering MATLAB-matrix Similar Reads How to Create a Nested For Loop in R? A loop in a programming language is a sequence of instructions executed one after the other unless a final condition is met. Using loops is quite frequent in a program. Need of a loop Let us consider a scenario where we want to print natural numbers from 1 to 3. We can simply print them one by one. 6 min read How to Create a New Matrix From All Possible Row Combinations in MATLAB? A  matrix represents a collection of numbers arranged in an order of rows and columns. A matrix encloses the elements in parentheses or brackets.  We will learn how to create a new matrix from all possible row combinations of rows of a different matrix and display the combinations. Procedure of Mak 3 min read How to Permute the Rows and Columns in a Matrix on MATLAB? In this article, we will discuss how to find the permutation of the rows and columns in a Matrix with the help of multiple approaches Method 1In this approach, we are simply permuting the rows and columns of the matrix in the specified format of rows and columns respectively.  For column permutation 4 min read How To Export a Matrix as a CSV File in MATLAB? A CSV file - Comma Separated File, as the name suggests, is a file that stores data delimited by a ' , '. In MATLAB, there is a simple way of exporting a matrix to a csv file. The writematrix() function is used to write a matrix into any desired file type. The syntax is writematrix(<matrix_name 2 min read How to Create a For Loop with Range in R? In this article, we will discuss how to create a for loop with range in R Programming Language. For loop is used to iterate the elements over the given range. We can use for loop to append, print, or perform some operation on the given range of integers. Consider the below syntax of the for loop tha 2 min read How to create a matrix in R In this article, we will discuss What is a matrix and various methods to create a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data set that collects rows and columns. The matrix stores the data in rows and columns format. It is possible to access the data i 3 min read Like