a 1 2 3 4 5 2 3 4 5 6 3 2 3 3 3 5 6 7 8 9 n m sizea c0 d 0

a = [1 2 3 4 5; 2 3 4 5 6; 3 2 3 3 3; 5 6 7 8 9]; [n, m] = size(a); c=0; d =0; for i = 1:n for j = 1:m if a(i, j)==2 c = c+1; end if a(i, j)>=8 d= d+1; end end end e = c*d; f = c/d; What will be the values of c, d, e and f after execution of the script. Write a output code line at the end of the script using fprintf to appropriately display c, d, e, and f and their values. How would you modify the code using if/elseif combination and obtain the same result. Explain how the modification changes the program execution.

Solution

The number of 2\'s in the matrix is stored in c and the number of values greater than 8 is stored in d. So c will be 3 and d will be 2.

e = 6 and f = 1.5

The line for printing is

Changing to elseif:

if a(i,j)==2

c = c+1;

elseif a(i,j)>=8

d = d+1

end

 a = [1 2 3 4 5; 2 3 4 5 6; 3 2 3 3 3; 5 6 7 8 9]; [n, m] = size(a); c=0; d =0; for i = 1:n for j = 1:m if a(i, j)==2 c = c+1; end if a(i, j)>=8 d= d+1; end

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site