Create the following matrix with a nested for loop in MATLAB
Create the following matrix with a nested for loop in MATLAB
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
Solution
C = zeros(3,5)
for m = 1:3
for n = 1 : 5
c(m,n) = m*n
end
end.
Now here we have defined nested loops to iterate
1) First loop is to iterate for rows
2) Second loop is to iterate for colums.
Now here we have looped first one till 3 as we have three rows and second one till 5 as we have five colums.
Now about the elements of matrix . If we analyse the data first row data is table of 1 second is table of 2 and third is table of 3.
Hence m*n where m is 1 for first row and n will loop 5 times from 1 to 5 so we will get the value of table 1
m=2 for second row and n will loop 5 times from 1 to 5 so we will get 2*1, 2*2,2*3,2*4,2*5
Same goes for m= 3.
