create a matrix using nesteed loop in matlab

To create a matrix using nested loops in MATLAB, you can use the following code:

main.m
% Initialize variables
n = 4;  % number of rows
m = 3;  % number of columns
A = zeros(n,m);  % create a n x m matrix filled with zeros

% Nested loop to fill the matrix
for i = 1:n
   for j = 1:m
      A(i,j) = i*j;
   end
end

% Display the matrix
disp(A)
257 chars
15 lines

In this example, we initialize the variables n and m to the desired number of rows and columns, and create a n x m matrix A filled with zeros using the zeros() function. We then use a nested for loop to iterate over each element in the matrix and fill it with the product of its row and column indices. Finally, we display the matrix using the disp() function.

You can modify the code to suit your specific matrix creation needs by changing the initialization values or the formula to calculate each matrix element.

gistlibby LogSnag