**Info**

This question is closed. Reopen it to edit or answer.

3 views (last 30 days)

Show older comments

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

Guillaume
on 13 Feb 2019

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.

Stephen
on 1 May 2019

@Maximilian Schmidt: movesum is called inside the function max, and it is max is called with two output arguments. So you need to read the max documentation.

A function called inside another function, like movesum inside max in your example, returns exactly one output argument as an input argument to the wrapper function, so

your example:

[summa, index] = max(movsum(h, n, 'Endpoints', 'discard'))

is exactly equivalent to

tmp = movsum(h, n, 'Endpoints', 'discard');

[summa, index] = max(tmp)

lalit choudhary
on 17 Apr 2019

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

Walter Roberson
on 11 May 2020

When you have a vector of length nx to be taken in full windows of length n, sliding along 1 at a time, then you have nx - n + 1 full windows. For example, data = [1 2 3 4 5 6 7 8 9 10], n = 7, then nx = 10, nx - n + 1 is 10 - 7 + 1 = 4, corresponding to the windows 1:7, 2:8, 3:9, 4:10

This codes is specifically for the case where you only want full windows -- which is what the 'discard' option of movsum is intended to indicate, that you want to discard any sum that was made with a window that was not full (such as 5:10 not being the full length of 7)

Shubham Pandey
on 3 Apr 2020

function [s,i] = max_sum(v,n)

s = 0; i = 0;

len = length(v);

l = len-n+1;

if n>len

s = 0;

i = -1;

else

for j=1:l

if j==1

s = sum(v(j:n));

i=1;

end

if s<sum(v(j:n))

s = sum(v(j:n));

i = j;

end

if n<len

n=n+1;

end

end

end

end

Prasad Reddy
on 14 Apr 2020

function [summa,index] = max_sum(v,n) % defining a function with name "max_sum", input augements are (v,n) and out put augemnts are [summa,index]

[r,e]=size(v); % reading the size of vector "v" to "r,e"

summa=0; % asaining 0 to summa

index=-1; % assaining -1 to index, as they asked in the question

sums=zeros(1,e-(n-1)); % creating a zro vector of requirted size. you can try itby taling two or th trial cases

if n>e % checking if n is greatr than th length of the vector so that summa and index can be rturnd as

summa=summa; % 0 and -1 respectively as askeed in the question.

index=index;

else % if n is less than the length of vctor then

for i=1:e-(n-1) % for loop to run across the length of the vector

for j=0:n-1 % for loop to run across the required number of elements to be summed

sums(i)=sums(i)+v(i+j); % elments of sums which are initially zeros are being updated step by step with th sum of 'n' numbers in vector

end

end

[s,i]=max(sums); % reading th maximum valu and its indx from sums to 's' and 'i'

summa=s;

index=i;

end

end

asad jaffar
on 4 Apr 2019

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

Vikas Kumar
on 11 Jun 2019

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

if length(A)<n

summa = 0;

index = -1;

else

B = maxk(A,n)

summa = sum(B);

z = zeros(1,length(B));

m = [];

for i = 1:length(B)

z = find(A==B(i));

m = [m z]

end

T = sort(m)

index = T(1,1)

end

end

Jaimin Motavar
on 29 Jun 2019

function [summa,index]=max_sum(a,b)

n=length(a);

summa=0;

total=0;

if b>n

summa=0;

index=-1;

return

end

for i=1:(n-b+1)

for j=i:(b-1+i)

total=total+a(1,j);

end

if total>summa

summa=total;

index=i;

end

total=0;

end

end

Roshan Singh
on 15 Sep 2019

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

L=length(v);

p=L-(n-1);

k=0;

t=0;

for i=1:p

z=n+k;

k=k+1;

s=0;

for d=i:z

s=s+v(d);

end

t(i)=s;

end

if (n<=L)

[summa index]=max(t);

else

summa=0;

index=-1;

end

end

Hussain Bhavnagarwala
on 13 Mar 2020

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

if n>length(v)

summa = 0;

index = -1;

return

end

x = (length(v) - n) + 1;

tot =[];

t=0;

total = 0;

for k = 1:x

for i =1:n

total = total + v(t+i);

end

tot(k) = total;

t=t+1;

total = 0;

end

summa = max(tot);

ind= find(tot==max(tot));

index = ind(1);

end

Shashidhar Rai
on 15 Mar 2020

Edited: Walter Roberson
on 15 Mar 2020

I have written the code but it's showing error. Can anyone tell me what's wrong in it.

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

ii=1;

if length(v)

index=-1;

summa=0;

elseif length(v) ==n

index=1;

summa=sum(v);

else

summa=0;

start=1;

for ii =start : start +n-1

if summa < sum (v [start : start+n-1] )

summa=sum(v[ start : start +n-1] ) ;

index = start;

end

start =start +1;

if start

length(v) - n+1

break;

end

end

end

Guillaume
on 15 Mar 2020

To add to the problem Walter pointed out:

- the function has an input m that is never used, but use the undefined variable n instead.
- there's a ii loop that never uses ii.
- that loops will only ever do one iteration because the if start is always going to be true and break out of it.

Irfan Hussain
on 31 Mar 2020

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

leng = length(v);

if leng < n

summa = 0;

index = -1;

else

w = [];

summa = 0;

for i = 1:leng

if summa < sum(v(i:n));

summa = sum(v(i:n));

index = i;

% x = sum(v(i:n));

%w = [w ,x];

if n < leng

n = n + 1;

%else

% summa = max(w);

% index = find(x == );

% end

end

end

end

end

Shubham Pandey
on 3 Apr 2020

this code has some error, wont work correctly for this case:

[summa index]=max_sum([ 45 -32 -25 27 78 -9 32 -1 -85 2 -32 -81 -32 -14 -31 46 -70 87 94 ], 15)

Jaimin Patel
on 7 Apr 2020

function [X,Y] = max_sum(v,n) % X and Y are output arguments

X = -inf; %take -inf value of X for getting minus sum

p = n;

Y = 1;

if n > length(v)

X = 0;

Y = -1;

end

for i = 1:(length(v)-(n-1))

a = sum(v(i:p));

p = p + 1;

if a > X %condition for taking maximum sum of consecutive elements of vector

X = a;

Y = i;

end

end

simple code for solution...

Emine Ertugrul
on 13 Apr 2020

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

m=length(v);

if n>m

summa=0;

index=-1;

else

M = movsum(v,n,'Endpoints', 'discard')

[a,b]=max(M)

summa = a

index = b

end

end

Walter Roberson
on 13 Apr 2020

Garvit Kukreja
on 14 Apr 2020

I have written this code. Can anyone tell me what's wrong in it.

Error : Variable summa has an incorrect value. max_sum([ -91 -57 -45 -8 -21 8 75 -80 89 -36 59 -71 -57 14 3 88 -79 -68 -6 ], 5) returned sum = 325 and index = 7 which is incorrect...

function [s,i]=max_sum(a,b)

B = maxk(a,b)

[n m]=size(B);

[y z] = size(a);

f=[]; %empty matrix

kk=0; %counter

if z>=b; %no. of element in 'a' more than or equal to'b'.

s=sum(B)

for ii=1:z;

for jj=1:m;

if B(jj)==a(ii);

kk=kk+1;

f(kk)=ii ;

end

end

end

i=min(f)

else

s=0

i= -1

end

Garvit Kukreja
on 14 Apr 2020

Thanks for your response.

it is sum of deduced matrix 'B',

where B=Maxk(a,b) ( If a is a vector, then maxk returns a vector containing the k largest elements of a)

program worked for this command : [summa, index] = max_sum([1 2 3 4 5 4 3 2 1],3)

Ahmed J. Abougarair
on 21 Apr 2020

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

L=length(v);

p=L-(n-1);

k=0;

t=0;

for i=1:p

z=n+k;

k=k+1;

s=0;

for d=i:z

s=s+v(d);

end

t(i)=s;

end

if (n<=L)

[summa index]=max(t);

else

summa=0;

index=-1;

end

end

Ahmed J. Abougarair
on 21 Apr 2020

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

k =length(v);

if k==n

summa = sum(sum(v));

index =1;

return

elseif k<n

summa = 0;

index =-1;

return

else

z = length(v);

summa = 0;

index = -1;

if n <= z

for i = 1:(z - n + 1)

total = sum(v(i:n - 1 + i));

if total > summa

summa = total;

index = i;

end

end

end

end

Roshan Barnwal
on 27 Apr 2020

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

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

for j = 2:length(v) -2;

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

if summa<r

summa=r;

index = j;

end

end

%% this works well but in few cases it asks for some error. can any one help me to short out the problem?

Salman P H
on 30 Apr 2020

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

if n>length(v)

summa = 0;

index = -1;

return;

end

x=1;

y=n;

great = -inf;

while y<=(length(v))

z = sum(v(1,x:y));

if z>great

great=z;

a=x;

x=x+1;

y=y+1;

elseif great==z

a=x-(x-a);

x=x+1;

y=y+1;

else

x=x+1;

y=y+1;

end

end

summa = great;

index = a;

end

Olel Arem
on 30 Apr 2020

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

len_v=length(v);summa=0;poss=[];ii=1;jj=n;

if n>len_v

summa=0;

index=-1;

return

end

while jj<=len_v

poss(ii)= sum(v(ii:jj));

[summa,index]=max(poss);

ii=ii+1;

jj=jj+1;

end

Prithvi Shams
on 5 May 2020

Here's my version of the code.

While the problem can be solved with movsum() in a single line, it's recommended to utilize if and for loops as a learning exercise.

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

if n > numel(v)

summa = 0;

index = -1;

else

j = n;

for i = 1:(numel(v) - n + 1)

s(i) = [sum(v(i:j))];

j = j + 1;

end

if numel(max(s)) > 1

m = max(s);

[summa index] = m(1);

else

[summa index] = max(s);

end

end

anuj chaudhari
on 5 May 2020

Edited: anuj chaudhari
on 5 May 2020

function [a, b] = max_sum(v,n)

if ~isscalar(n)||n<0||n~=fix(n)

error('n must be a positive integer')

end

[~,c]=size(v);

d=length(v);

r = zeros(1,d-n+1);

if n>c

b=-1;

a=0;

else

for k=1:(d-n+1) %from here, this is a means of movsum

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

[a, b]=max(r);

end

end

end

Shandilya Kiran Bhatt
on 11 May 2020

You can also solve this question by using while loop if you don't know movsum function like me.Look at following code:-

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

b = length(A);

c = zeros(1,b-n+1);

i = 1;

if b < n

summa = 0;

index = -1;

else

while n<=b && i<=b

c(1,i) = sum(A(i:n));

n = n+1;

i = i+1;

end

[summa , index ] = max(c);

end

Tahsin Oishee
on 17 May 2020

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

summa=0;

index=0;

w=1;

[b a]=size(v);

if n>a

index=-1

summa=0;

else

if n<=a

for i=1:(a+1-n)

sum=0;

for j=1:n

sum=sum+v(w);

w=w+1;

end

w=w-n+1;

if sum>summa;

summa=sum;

index=v(i);

end

summa

index

end

end

end

end

Walter Roberson
on 17 May 2020

You already have two loop control variables, i and j: use 2D indexing instead of doing strange things with w.

We recommend against naming a variable sum: it is very common to want to use the sum() function after having used sum as a variable.

khyathi beeram
on 18 May 2020

Edited: khyathi beeram
on 18 May 2020

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

a=[ ];

if n<=length(v)

for i=1:length(v)-(n-1)

sum=0;

for j=i:i+n-1

sum=sum+v(j);

end

a(i)=sum;

end

summa=max(a);

g=find(a==summa);

index=g(1,1);

else

summa=0;

index=-1;

end

end

vighnesh rana
on 19 May 2020

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

if length(A)< n

summa = 0;

index = -1;

return;

end

summa = -inf;

index = -1;

for i = 1:(length(A)-n+1)

total = sum(A(i:(i+n-1)));

if total > summa

summa = total;

index = i;

end

end

end

Taif Ahmed BIpul
on 20 May 2020

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

if size(v,2)<n

summa=0;

index=-1;

return

end

p=size(v,2)-(n-1);

k=1;

A=zeros(1,n);

summ1=zeros(1,p);

for i=1:p

jj=i;

while jj<=(i+n-1)

while k<=n

A(k)=v(jj);

jj=jj+1;

k=k+1;

end

end

summ1(i)=sum(A);

k=1;

end

summa=max(summ1);

index1=find(summ1==max(summ1));

if size(index1,2)>1

index=index1(1);

return

else

index=index1;

end

utkarsh singh
on 21 May 2020

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

if numel(m)<n

summa=0;

index=-1;

else

summa=-inf;

for i=1:(numel(m)-n+1)

j=m(i:i+n-1);

maxsumma=sum(j);

if(maxsumma>summa)

summa=maxsumma;

index=i;

end

end

end

end

sai teja
on 28 May 2020

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

% If n is greater than v return the specified values

% Using return keyword exits the function so no further code is

% evaluated if n > length(v) summa = 0; ind = -1;

return;

end

% Initialize summa to -inf.

% Then work through the vector, checking if each sum is larger than the

% current value of summa summa = -inf; ind = -1;

% Once we get to length(v)-n+1

we stop moving through the vector for ii = 1:length(v)-n+1

currentV = v(ii:(ii+n-1));

currentSumma = sum(currentV);

% If currentSumma greater than summa, update summa and ind

if currentSumma > summa

summa = currentSumma;

ind = ii;

end

end

end

Kumar Shubham
on 2 Jun 2020

Edited: Kumar Shubham
on 2 Jun 2020

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

if n > length(v)

summa = 0;

index = -1;

return;

elseif n<=length(v)

a=movsum(v,[0,n-1]);

b=a(1:length(v)-n+1);

[summa,index]= max(b);

end

end

Sumit Kumar Sharma
on 2 Jun 2020

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

l=length(v);

if n>l

summa=0;

index=-1;

else

p=l-n+1;

ii=0;

total=-inf;

for j=1:p

s=sum(v(j:n+ii));

ii=ii+1;

if s>total

total=s;

index=j;

else

continue;

end

end

summa=total;

end

end

KAVITI BHARGAV RAM NAIDU
on 3 Jun 2020

Edited: KAVITI BHARGAV RAM NAIDU
on 5 Jun 2020

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

b=zeros(1,length(v)-n+1); % initializing the b vector(only zeros) of length = length(v)-n+1

% this will decrease time taken for getting answer

if n > (length(v)) % if n is larger than length of v sum = 0 and index = -1 as per question (last line)

summa = 0 ;

index = -1;

else

for p = 1:[length(v)-n+1]

b(1,p) = sum (v(p:p+n-1)); % b(1,1)= v(1,1)+v(1,2) (if n = 2)

end % giving values to vector b using sum function

[summa index] = max(b); % if n = 2 sum should be between 2 consecutive elements.

end % if p = 2 and n = 2 sum between 2 nd and 3rd element should be considered

% p+n-1 = 2+2-1 = 3 // sum (v(2:3)) means sum of 2nd and 3rd element of vector b.

% so sum(v(p:p+n-1))

zineb britel
on 4 Jun 2020

%what's wrong with my code

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

index=0; summa=0;

if n > length(v)

summa= 0;

index=-1;

else

a=sort(v,'descend');

summa= sum(a(1:n));

index_row= find(v>=a(n));

index= min(index_row);

end

end

Ujjawal Barnwal
on 8 Jun 2020

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

summa=sum(v(v<0));

for ii=1:(length(v)-(n-1))

t=0;

for jj=ii:(ii+(n-1))

t=t+v(jj);

end

if (t>summa)

summa=t;

index=ii;

end

end

end

AYUSH MISHRA
on 13 Jun 2020

Edited: AYUSH MISHRA
on 13 Jun 2020

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

if n > length(v)

summa = 0;

index = -1;

return;

end

summa = -inf;

index = -1;

for ii = 1:length(v)-n+1

currentV = v(ii:(ii+n-1));

currentSumma = sum(currentV);

if currentSumma > summa

summa = currentSumma;

index = ii;

end

end

end

Maddireddy Harshavardhan Reddy
on 18 Jun 2020

Create a row vector named x that starts at 1, ends at 10, and contains 5 elements.

Sneha Paul
on 19 Jun 2020

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

z=[];

if n>length(v)

summa=0;

index=-1;

return

elseif (n<=length(v))

for i=1:length(v)-(n-1)

z(i)=sum(v(i:i+(n-1)));

end

end

[summa,index]=max(z)

MICHAEL
on 19 Jun 2020

function [s,i] = max_sum(v,n)

s = 0; i = 0;

len = length(v);

l = len-n+1;

if n>len

s = 0;

i = -1;

else

for j=1:l

if j==1

s = sum(v(j:n));

i=1;

end

if s<sum(v(j:n))

s = sum(v(j:n));

i = j;

end

if n<len

n=n+1;

end

end

end

end

Sakib Javed
on 22 Jun 2020

Edited: Sakib Javed
on 22 Jun 2020

ffunction [summa index] = max_sum(A,n)

m=length(A);

if n > m

summa = 0;

index = -1;

return;

end

B = [];

q=m-(n-1);

for ii = 1:q

B=[B sum(A(ii:ii+n-1))];

end

[summa index] = max(B);

end

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

Start Hunting!