Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Nested loops are often used to make tables and matrices as in the following exam

ID: 3143911 • Letter: N

Question

Nested loops are often used to make tables and matrices as in the following example.

% Prints a matrix of 1's.

rows = 4;

columns = 2;

% for loop over the rows

for i=1:1:rows

for j=1:1:columns

fprintf('1')

end

fprintf( ' ' ) % Go to next line (try leaving this command out to see what it does)

end

Write a function that computes the average of all of the values in a two-dimensional matrix of any size. Use loops rather than built in functions to compute the average. Output the average value with the text The average value of the matrix elements is" X where X is the computed value. Report X to one decimal place, i.e. 7.3 not 7.39. Call the function with avgMat(mat) where mat is a two-dimensional matrix that has been defined.

Explanation / Answer

use the code give below subsituting the value of m and n according to you for a m x n matrix.

for i = 1 : m

for j= 1 : n

total =total + a[i][j];

end

end

avg = total/mn;

% for printing upto one decimal use sprintf

sprintf('1.1f', avg)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote