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

10 ビュー (過去 30 日間)
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 = 0
index = -1
else
summa = -inf
index = 0
for 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)
end
end
end

採用された回答

Stephen23
Stephen23 2020 年 8 月 5 日
編集済み: Stephen23 2020 年 8 月 6 日
The main bug is that you are iterating over data values and not indices as your code requires. On this line you define the loop iterator ii to be elements of v, i.e. data values:
for ii= v(1:(numb_elements +1 - n))
but then within your code you use ii as an index. It is trivial to fix by iterating over the indices:
for ii = 1:(numb_elements+1-n)
Your code needs to be much more consistently written, in terms of alignment and spacing. For example the whitespace between operators and numbers, compare:
>> [1 - 2]
ans = -1
>> [1 -2]
ans =
1 -2
>> [1-2]
ans = -1
Sometimes you wrote your code with whitespace around binary operators and sometimes none, which will at some point cause you errors. Solution: write neater, more consistent code. Pick one style and stick with it. Do not mix.
Also align your code consistently:
function [summa index]=max_sum(v,n)
numb_elements = length(v)
if n > (numb_elements)
summa = 0;
index = -1;
else
summa = -inf;
index = 0;
for ii = 1:(numb_elements+1-n)
sum_n = sum(v(ii:(ii+n-1)));
if sum_n > summa
summa = sum_n;
index = v(ii);
end
end
end
Note: I have not checked that this code gives the correct answer (after all, it is your homework), only that it runs without error on the example that you have given.
  2 件のコメント
nicola Braham
nicola Braham 2020 年 8 月 5 日
Thank you soooo much! I thought I had got myself muddled with index position and variable ..but I was so focussed on getting the index part right that I assumed all my errors were related to this and didn't even look back at that.
it still wasn't perfect but have sorted it out now.. finally.. thank God!!
Gosh that was painful.
The problem is that I am learning all of this from scratch very quickly so whilst everything makes complete sense to me as I am learning it but by the time I get around to the assignments I have forgotten most of what I thought I knew.. I just need a lot moren practice.
Thank you though!
Stephen23
Stephen23 2020 年 8 月 6 日
@nicola Braham: I hope it helped. You can show your appreciation by accepting my answer :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by