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

Matlab Programming help! Please provide the code for following question. An n-by

ID: 2081091 • Letter: M

Question

Matlab Programming help! Please provide the code for following question.

An n-by-n square matrix is called a "sparse" matrix if more than half of its elements are zero. Given an n-by-n matrix A, write a program that determines if A is sparse or not. The program should also determine the number of zero elements that lie in the main diameter, in the upper triangle (excluding the diameter), and in the lower triangle (excluding the diameter) of the matrix A. Use the variables Z_D, Z_UT, and Z_LT to count the zeros at each aforementioned region (no display of these variables is needed).

Explanation / Answer

Matlab script:

function [Z_D,Z_UT,Z_LT,sparse1]=num_of_zeros(A)
Z_D = 0;
Z_UT=0;
Z_LT = 0;
sparse1 = 'no';
for i=1:length(A)
for j=1:length(A)
if i<j
if A(i,j)==0
Z_UT = Z_UT+1;
end
elseif i>j
if A(i,j)==0
Z_LT = Z_LT+1;
end
else
if A(i,j)==0
Z_D = Z_D+1;
end
end
end
end
tot_zeros = Z_D +Z_UT+Z_LT;
if tot_zeros>((length(A)^2)/2)
sparse1 = 'yes';
end
end

command window log:

>> A = [1 0 1 1;0 1 0 0;1 0 1 0;0 0 0 1]

A =

1 0 1 1
0 1 0 0
1 0 1 0
0 0 0 1

>> [Z_D,Z_UT,Z_LT,sparse1] = num_of_zeros(A)

Z_D =

0


Z_UT =

4


Z_LT =

5


sparse1 =

yes

>>