Problem 32. Most nonzero elements in row Given the matrix a, return the index r
ID: 3580350 • Letter: P
Question
Problem 32. Most nonzero elements in row Given the matrix a, return the index r of the row with the most nonzero elements. Assume there will always be exactly one row that matches this criterion. Example: Input a 1 2 0 0 0 0 0 5 0 0 2 7 0 0 0 069 3 3 1 output r is 4 Solution BE MATLAB Documentation function r fullest row (a) 3 end Click submit to create a new solution. Submitting an incorrect solution will not display Submit your name to other players, and it does not affect your score By clickng Submit you agree to our Terms of UseExplanation / Answer
A)
function r=fullest_row(a)
r=1
nz=0
nz1=0
[row,col]=size(a)
for i=1:row+1
nz1=0
for j=1:col+1
if(a(i,j)!=0)
nz1=nz1+1
end
end
if(nz1>nz)
nz=nz1
r=i
end
end
end
B)
function b=back_and_forth(n)
b=ones(n)
num=1
for i=1:n+1
for j=1:n+1
if(i%2==1)
b(i,j)=num
end
if(i2==0)
b(i,n-i+1)=num
end
end
num=num+1
end
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.