Asked by Alexander Villanueva
on 26 Jun 2019

The question is this:

Write a function called max_sum that takes v, a row vector of numbers, and n, a positive integer as inputs. The function needs to find the n consecutive elements of v whose sum is the largest possible. In other words, if v is [1 2 3 4 5 4 3 2 1] and n is 3, it will find 4 5 and 4 because their sum of 13 is the largest of any 3 consecutive elements of v. If multiple such sequences exist in v, max_sum returns the first one. The function returns summa, the sum as the first output argument and index, the index of the first element of the n consecutive ones as the second output. If the input n is larger than the number of elements of v, the function returns 0 as the sum and -1 as the index. Here are a few example runs:

[summa, index] = max_sum([1 2 3 4 5 4 3 2 1],3)

summa = 13

index = 4

[summa, index] = max_sum([1 2 3 4 5 4 3 2 1],2)

summa = 9

index = 4

THe code I have written so far is as follows. My problem is that I am stuck on obtaining the largest possible sum. I wrote code and have been able to generate different sums in a vector but I am unable to put those answers in a vector themselves, by putting the answers in a vector I want to be able to use the 'max' function to obtain the largest sum. I haven't even touched finding the 'index' portion yet. Here's my code:

function [summa,index] = max_sum(v,n)

r = 0;

summa = zeros(1,length(v)); %% created a vector of 0's to length of v

if n > length(v)

summa = 0;

index = -1;

else

while n <= length(v)

summa = sum(v(r+1:n)) % here need to replace 0's with the sum (with logical index) so i can choose max

r = r+1;

n = n+1;

end

summa = max(summa);

index = n;

end

%summa = v;

%index = n;

end

Again, I need some help because I am unsure how to proceed from here. Thank you.

Answer by James Tursa
on 27 Jun 2019

Edited by James Tursa
on 27 Jun 2019

Accepted Answer

Some issues:

n is a fixed input ... you should not be changing n inside your function. Get rid of that n = n + 1 statement.

The values you are summing v(r+1:n) isn't correct. You need to be summing n consecutive numbers. So, assuming the first index of these numbers is r, the subset would be v(r:r+n-1)

The loop needs to run as long as that last index of r+n-1 isn't greater than numel(v).

Prior to the loop, you should initialize summa and index to default values (e.g., maybe summa = -inf, or perhaps the sum of the first sequence of numbers in the vector). The summa variable is a scalar value (see the examples), not a vector, so you shouldn't be initializing it to a vector of zeroes.

Inside the loop you should have something like this logic:

if( the sum of the v(r:r+n-1) elements is greater than the current value of summa )

save the sum in summa and save the r in index

end

Give a try at making these changes and then post replies on this thread if you have more problems.

Alexander Villanueva
on 27 Jun 2019

Thank you for the clarity, I have updated the code and had another issue regarding giving me the first index if there are consecutive sums but I was able to figure that part out. However another issue popped up. The problem is that if 'n' is greater than numel(v), the answer is supposed to be summa = 0 and index = -1. However, when I do a test run where n is greater than the length(v), that answer does not appear, rather it does give summa = 0 and index = -1 but there is also an error that appears alongside it that says "Index exceeds the number of array elements". I thought the reason for having my first "if' statement was to take care of this issue yet it does not appear to be working. Here's the updated code:

function [summa,index] = max_sum(v,n)

r = 1;

summa = 0;

index = 0;

if n >= length(v)

summa = 0

index = -1

else

while n < numel(v)

sum(v(r:r+n-1));

r = r+1;

if sum(v(r:r+n-1)) == sum(v(r-1:r-1+n-1))

r=r-1;

break

elseif sum(v(r:r+n-1)) > sum(v(r+1:r+1+n-1))

break

end

end

end

index = r;

summa = sum(v(r:r+n-1));

end

James Tursa
on 27 Jun 2019

This:

if n >= length(v)

should be this:

if n > length(v)

This:

while n < numel(v)

doesn't match my suggestion:

while( that last index of r+n-1 isn't greater than numel(v) )

And this:

if sum(v(r:r+n-1)) == sum(v(r-1:r-1+n-1))

doesn't match my suggestion either:

if( the sum of the v(r:r+n-1) elements is greater than the current value of summa )

Also, your entire if-elseif-end block is more complicated than my suggestion of a simple if-end block, so look at that again.

So, take a look at fixing these up and then get back to us with your progress.

Sign in to comment.

Answer by Mukti Awad
on 16 Aug 2019

I am getting this error while executing code

- Assessment result: incorrect[1 2 3 4 5 4 3 2 1]max_sum([ 1 2 3 4 5 4 3 2 1 ], 10) returned the following MATLAB error: Index exceeds the number of array elements (9).
- Assessment result: incorrectrandom vectorsVariable summa has an incorrect value. max_sum([ 71 -54 17 60 -51 -3 98 -65 -61 35 90 94 -11 -69 82 -86 -12 ], 5) returned sum = 121 and index = 3 which is incorrect...

James Tursa
on 16 Aug 2019

Mukti Awad
on 18 Aug 2019

Code is here:

function [summa,index] = max_sum(v,n)

r = 1;

summa = 0;

index = 0;

if n > length(v)

summa = 0;

index = -1;

else

while n < numel(v)

sum(v(r:r+n-1));

r = r+1;

if sum(v(r:r+n-1)) == sum(v(r-1:r-1+n-1))

r=r-1;

break

elseif sum(v(r:r+n-1)) > sum(v(r+1:r+1+n-1))

break

end

end

end

index = r;

summa = sum(v(r:r+n-1));

end

Getting error:

Assessment: 0 of 2 Tests Passed

More Info

Assessment result: incorrect[1 2 3 4 5 4 3 2 1]

max_sum([ 1 2 3 4 5 4 3 2 1 ], 10) returned the following MATLAB error:

Index exceeds the number of array elements (9).

Assessment result: incorrectrandom vectors

Variable summa has an incorrect value.

max_sum([ -81 -77 -45 -68 7 85 -9 30 89 62 83 83 94 -77 52 43 -62 -76 -2 26 -80 ], 2) returned sum = 92 and index = 5 which is incorrect...

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.