MATLAB Answers

0

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.

Asked by Ajai Kannan on 13 Feb 2019
Latest activity Answered by lalit choudhary on 17 Apr 2019 at 20:49
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.
I tried the follwing code but when there are two occurences for the same number, they both get removed in the same iteration and thus causes trouble.
function [summa, index] = max_sum(v,n)
summa = 0;
i = 0;
j = v;
m = [];
while i < n
summa = summa + max(j);
b = find(v==max(j));
m = [m b];
j = j(j<max(j));
i = i + 1 ;
end
t = sort(m);
index = t(1,1);
end

  2 Comments

bro do you have the code?
i am using movsum and then max but what about index? how to find the index

Sign in to comment.

3 Answers

Answer by Guillaume
on 13 Feb 2019
 Accepted Answer

Don't use the question title to post the content of your assignment as that get truncated.
Assuming that your assigment is to find the sequence of length n of consecutive numbers with the highest sum, then I don't see how your algorithm even attempts that. You don't care about the maximum of the vector, it's completely irrevelant to the sequence sum. You want to calculate a sliding sum (i.e. first sum(v(1:n)), then sum(v(2:n+1)), etc. and find the maximum of these.
If you're allowed to use movsum, it can be trivially done in just two lines.

  17 Comments

@asad jaffar: You have found out, that movsum replies a vector of the same size as the input. Then there are two ways to solve the problem:
  1. by maths: If movsum replies the sum of the surrounding n elements, you have to subtract ceil(n/2) from the index and ignore the leading and trailing elements of the sum.
  2. by specific command: instruct movsum to reply the sum only for elements, which have n neighbors: Use the 'Endpoints', 'discard' feature.
You find the latter solution, if you read the documentation. Do this, because it is unlikely, that you can guess all required details.
why you guys dont post the running code which you guys have tested
Because we don't usually give solutions to homework problems, particularly ones such as easy as this.
Spending a little bit of time reading about the movsum function, particularly the name/values optional arguments you could have figured out the solution:
function [summa, index] = max_sum(v,n)
[summa, index] = max(movsum(v, n, 'Endpoints', 'discard'));
end
Really not hard. I was wrong in my initial answer. You don't even need two lines, you need just one.
edit: I've just seen that Jan and Walter had already given you all the information required to write the above. Really you have no excuse for not figuring it out yourself.
There is also an efficient implementation with cumsum(), but that would be better with two lines of code instead of 1.

Sign in to comment.


Answer by asad jaffar on 4 Apr 2019 at 15:04

jan and walter take a look at this code this is giving me correct answers except for negative elements array
function [summa, index]=max_sum(b,n)
y=length(b)
q=movsum(b,n)
[maxsum,maxidx]=max(q)
summa=maxsum
index=(maxidx-floor(n/2))
if n==y
j=1
end
end
command window
max_sum([ 79 15 -18 -28 -30 52 -81 31 -74 4 57 -96],4)
y =
12
maxsum=
94 76 48 -61 -24 -87 -28 -72 -120 18 -109 -35
summa =
94
index =
1
index =
1
but i think my code is giving the right value plz take a look and tell me what to do

  5 Comments

I have posted working code in my answer above. This is getting tiresome.
i want to say sorry @guillaume the code worked thanks ,and i am sorry i said that your code is wrong ,thanks buddy .can you explain me what does endpoints and discard do how it is working i want to learn .
@asad: Again: Read the documentation of movsum:
doc movsum
Then try it by your own in the command windows, e.g.:
x = rand(1, 6)
movsum(x, 2)
movsum(x, 2, 'Endpoints', 'discard')
You asked: "are guys even do coursera" - no, of course we do not solve a course for beginners. we have done this about 20 or 30 years ago (a bold but maybe matching guess). We are not going to do exactly what you are doing only to answer very easy programming questions.
"i am waiting" - this is definitely the wrong approach. Exhaustive hints and working code have been posted some hours earlier already.
Some comments to your code:
function [summa, index]=max_sum(b,n)
y=length(b) % This is not useful
q=movsum(b,n)
[maxsum,maxidx]=max(q)
summa=maxsum % Why not directly: [summa,maxidx]=max(q)
index=(maxidx-floor(n/2))
if n==y % While j is not used anywhere, omit this
j=1
end
end
A nicer version:
function [summa, index] = max_sum(b, n)
q = movsum(b, n);
[summa, maxidx] = max(q);
index = maxidx - floor(n / 2);
end
But the result is not correct: The first and last two elements of the sum (or in general: floor(n / 2) elements) are not the sum of n elements of the input vector. See: doc movsum. A solution:
function [summa, index] = max_sum(b, n)
q = movsum(b, n);
m = floor(n / 2);
[summa, index] = max(q(m+1:end-m+1));
end
Try to understand, why q is cropped here. Instead of cropping it is smarter to let movsum exclude the marginal elements already with setting 'Endpoints' to 'discard'.

Sign in to comment.


Answer by lalit choudhary on 17 Apr 2019 at 20:49

After hours of brainstorming, i finally figured it out
try this code-
function [summa, index]=max_sum(b,n)
y=length(b)
if n>y
summa=0;
index=-1;
else
[summa, index] = max(movsum(b, n, 'Endpoints', 'discard'));
end
end

  0 Comments

Sign in to comment.