MATLAB: Output argument “” (and maybe others) not assigned during call to “”..

#not assigned #output argument

Hello,
I guess my code is right but i get the above mentioned error. Someone please help me solve the problem….
I have attached the code below
function [summa , index] = max_sum(M,n)
b=size(M);total = 0;comp = 0;
if n>b(1,2)
index = -1;summa = 0;return
else
for i = 1:b(1,2)-n+1
total = 0;
for a = 0:n-1
total = total+M(i+a);
end
if total>comp
comp = total;index = i;
end
end
summa = comp;
end
end

Best Answer

you start comp at 0. What happens if the running total of contents is always less than 0? then your if is never true and you do not assign to index