Write a function max_sum that takes v a row vector of numbers & n,a positive integer as inputs.The function needs to find n consecutive elements of v whose sum is largest poss
5 次查看(过去 30 天)
显示 更早的评论
I write this code for the qst in desciption
function [summa,index] = max_sum(v,n)
l=length(v);
if n>length(v)
summa =0
index=-1
else
m=v(1:n)
for ii=1:l-n+1
vect=v(ii:ii+n-1)
if sum(m)<sum(vect)
m=vect
end
end
summa = sum(m)
index = m(1)
end
end
and it's working for all exemple in description but for long vector like in qst 2 the result it's false
the problem is the rihjt response it's my, bcz I check it and I don't know if someone can help me if I already didn't undestand the problem
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!