Complete the code to create a nested loop that prints numbers 1 to 3 for both loops.
for i = 1:3 for j = 1:[1] disp([i, j]) end end
The inner loop must run from 1 to 3 to match the outer loop and print pairs from 1 to 3.
Complete the code to sum all elements in a 2D matrix using nested loops.
total = 0; for row = 1:size(matrix, 1) for col = 1:size(matrix, [1]) total = total + matrix(row, col); end end
The second dimension of the matrix is the number of columns, which is size(matrix, 2).
Fix the error in the nested loops that print the multiplication table from 1 to 5.
for i = 1:5 for j = 1:[1] fprintf('%d x %d = %d\n', i, j, i*j); end end
The inner loop must run from 1 to 5 to complete the multiplication table for numbers 1 to 5.
Fill both blanks to create a nested loop that fills a matrix with the product of its indices.
rows = 3; cols = 4; result = zeros(rows, cols); for r = 1:[1] for c = 1:[2] result(r, c) = r * c; end end
The outer loop runs over rows, the inner loop over columns to fill the matrix correctly.
Fill all three blanks to create a nested loop that counts how many elements in a matrix are greater than 10.
count = 0; for i = 1:[1] for j = 1:[2] if matrix(i, j) [3] 10 count = count + 1; end end end
The loops iterate over all rows and columns. The condition checks if elements are greater than 10.