clc clear all % use of nested loops % nested loop structure disp('Nested loop structure') for i=1:1:2 for j=1:1:4 for k=1:1:4 fprintf('i=%g, j=%g, k=%g\n',i,j,k) end end end % Example 1 disp(' ') disp('Example 1') disp('Generate an Identity matrix of any size') num=6; % number of rows and columns for i=1:1:num for j=1:1:num if i==j I(i,j)=1; else I(i,j)=0; end end end disp('The generated matrix is:') disp(I) % Example 2 disp('Example 2') disp('Generate a special matrix') % INPUTS row=3; col=4; for i=1:1:row for j=1:1:col S(i,j)=j; end end disp('The special matrix is:') disp(S) % Example 3 disp('Example 3') disp('Generate a special matrix') % INPUTS row=4; % row number, can change col=5; % column number, can change for i=1:1:row for j=1:1:col A(i,j)=(i-1)*col+j; end end disp('Special Matrix:') disp(A) % Alternative method to generate the above matrix disp('Example 3 - Alternative Method') for c=1:1:col for r=1:1:row a(r,c)=(r*col-col)+c; end end disp('Alternative Method Special Matrix:') disp(a)