MATLAB: Problem 32. Most nonzero elements in row

codyfunction

function r = fullest_row(a)
x=sum(a==0);
y=max(x);
row=0;
for i=1:length(x)
if x(i)==y
row=i;
end
r=row;
end
whats wrong with the code???

Best Answer

Probably this line
x=sum(a==0);
should really be this
x=sum(any(a),2);
The whole thing looks like it could be done in 1 line
[~,row]=max( sum(any(a),2) );