Hi, this function is to return the maximum value of 'n' consecutive elements but error index exceeds the number of array elements appears for a random applied vector
when applied to random vectors:
max_sum([ 26 81 -93 27 -93 74 9 94 80 -95 -99 87 -13 41 6 84 -91 70 84 -4 42 78 ], 12) returned the following MATLAB error: Index exceeds the number of array elements (22).
Here is my function… where am i going wrong? Thank you so much.
function [summa index]=max_sum(v,n)numb_elements = length(v)n_elements =[]if n> (numb_elements)summa = 0index = -1else summa = -inf index = 0for ii= v(1:(numb_elements +1 - n)) n_elements = v(ii:(ii+n-1)) sum_n = sum (v(ii:(ii+ n-1))) if sum_n > summa summa = sum_n index = v(ii) endendend
Best Answer