Write a function max_sum that takes v a row vector of numbers & n,a positive integer as inputs.The function needs to find n consecutive elements of v whose sum is largest possible.It returns summa & index of first element of n consecutive integers.

조회 수: 186 (최근 30일)
%Example-[summa,index]=max_sum([1 2 3 4 5 4 3 2 1],3)
% summa=13
% index=4
function [summa,index]=max_sum(v,n)
total=v(1,1);
if n>v
summa=0;
index=-1;
else
for ii=1:length(v)
jj=ii+(n-1);
if jj<=length(v);
total=[total,sum(v(ii):v(jj))];
end
[summa,index]=max(total);
end
end
  댓글 수: 12
Vivek Mishra
Vivek Mishra 2022년 1월 23일
would you please explain the line : total=v(1,1); and total=[total,sum(v(ii):v(jj))];
khalid
khalid 2023년 7월 14일
function [summa,index]=max_sum(v,n)
summa=1;
c=size(v,2);
p=0;
if(n<=c)
for jj=1:(c-n+1)
s=0;
for ii=jj:jj+n-1
s=s+v(1,ii);
end
p = v(1,jj);
if(s>summa)
summa=s;
index=p;
end
end
else
summa = 0;
index =-1;
end
end

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2020년 5월 19일
편집: KSSV 2020년 5월 19일
v = [1 2 3 4 5 4 3 2 1] ;
n = 3 ;
N = length(v) ;
sumn = zeros(1, N - n + 1); % Pre-allocation
for i = 1:N - n + 1
sumn(i) = sum(v(i:(i+n-1))) ;
end
[val,idx] = max(sumn)
  댓글 수: 22
Walter Roberson
Walter Roberson 2021년 8월 6일
There is a possibility that there is more than one location that is the maximum. The find() step returns all of the locations and min() of the find returns the first. Another way of doing that would be to use find(Summa==S, 1)

댓글을 달려면 로그인하십시오.

추가 답변 (22개)

Willynx Vixamar
Willynx Vixamar 2020년 11월 13일
function [summa, index] = max_sum(v,n)
L = length(v);
S=zeros(1,L-n+1);
if n > L
summa = 0;
index = -1;
return
else
for i = 1:(L-n+1)
S(i)=sum(v(i:(i+n-1)));
end
summa = max(S);
ind = find(S == summa);
index = min(ind);
end
end
  댓글 수: 6
MINH
MINH 2023년 6월 30일
Can somebody help me explain this part of the code :
ind = find(S == summa);
index = min(ind);
Cheers,
DGM
DGM 2023년 6월 30일
편집: DGM 2023년 6월 30일
The first line returns the linear indices of S where S==summa.
The second line returns the minimum of those indices.
Both lines are unnecessary, because max() will return that index if you actually request it. Why do all this:
summa = max(S);
ind = find(S == summa);
index = min(ind);
... when you can just do:
[summa index] = max(S);

댓글을 달려면 로그인하십시오.


Rik
Rik 2020년 7월 6일
편집: Rik 2020년 7월 30일
You can get a speedup by using a convolution to calculate the moving sum:
v=randi(100,2000,1);
n=10;
clc
timeit(@() option1(v,n))
ans = 3.4287e-04
timeit(@() option2(v,n))
ans = 3.5765e-05
function [val,idx]=option1(v,n) % function as suggested by KSSV
if n>numel(v),val=0;idx=-1;return,end %my addition
N = length(v) ;
sumn = zeros(1, N - n + 1); % Pre-allocation
for i = 1:(N - n + 1)
sumn(i) = sum(v(i:(i+n-1))) ;
end
[val,idx] = max(sumn);
end
function [val,idx]=option2(v,n)
if n>numel(v),val=0;idx=-1;return,end
sumn=conv(v,ones(n,1),'valid');
[val,idx] = max(sumn);
end
  댓글 수: 1
DGM
DGM 2023년 3월 8일
편집: DGM 2023년 3월 8일
Edited only to demo the code.
An order of magnitude improvement and it's nice and concise -- what's not to like?

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2020년 7월 29일
편집: Bruno Luong 2020년 7월 29일
N=3;
v = [1 2 3 4 5 4 3 2 1];
[s,index] = maxsum(v, N)
Using this function
%% NOTE this function returns empty outputs if N>length(v)
% might be more sensitive to numerical error
function [s,index] = maxsum(v, N);
c=cumsum([0; v(:)]);
[s,index]=max(c(N+1:end)-c(1:end-N));
end
Result
s =
13
index =
4
>>
  댓글 수: 4
Rik
Rik 2020년 7월 30일
Thanks for the correction. I tested it for random lengths random vectors with random N against your code, and it seems to match now.
The fix doesn't affect the timing.
Bruno Luong
Bruno Luong 2020년 7월 30일
편집: Bruno Luong 2020년 7월 30일
The faster timing of CONV for large array can be perhaps explained by multi-thread of the engine, that cannot be carrierd out with CUMSUM, which is sequential calculation.
TMW comes from far, the CONV in the earlier years suffered from performance. Now they have improved it greatly.
In any case a factor of 0.7 1.5 between two methods are to me essentially ... 1.
In any case your code get a vote from me.

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2020년 8월 1일
function [s,index] = maxmovsum(v, N)
c = movsum(v,N);
[s,index] = max(c(1+floor(N/2):end-floor((N-1)/2)));
end

tharak infinity
tharak infinity 2020년 11월 10일
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
  댓글 수: 3
OUSSAMA El GABBARI
OUSSAMA El GABBARI 2022년 1월 24일
Could you explain the summa = -inf; line ?
Is -inf a built in keyword or something ?
Rik
Rik 2022년 1월 24일
inf is the keyword for infinity. Negative infinity is guaranteed to be the lowest value possible.

댓글을 달려면 로그인하십시오.


Sahil Deshpande
Sahil Deshpande 2020년 12월 17일
function [summa,index] = max_sum(v,n)
l=length(v);m=0;t=0;pointer=1;index=0;
%When n is larger than the length of vector v
if n>l
summa=0;
index=-1;
else
%When n is smaller than the length of vector
for a = 1:n
m=m+v(a);
end
for i = 1:(l-(n-1))
k=i;t=0;
for j = 1:n
t = t+v(k);
k=k+1;
end
if t>m
m=t;
pointer=i;
end
end
summa=m;
index=pointer;
end
I have written this code without using any inbuilt functions, just using loops. It works great!
I know it looks kinda ugly, can anyone help me optimize this code and make it shorter?
  댓글 수: 1
Rik
Rik 2020년 12월 17일
Have you looked at the other solutions in this thread?
Also, you already used a builtin function on your first line: length.

댓글을 달려면 로그인하십시오.


Shantanu Nighot
Shantanu Nighot 2020년 12월 22일
% Here is the working code for This Problem statement
function [summa index] = max_sum(v,n)
ii = 1;
jj = n;
% Initialize summa to zero
summa = 0;
% if n is greater than number of elements in v the return summa = 0, index = -1
if n>numel(v)
summa = 0;
index = -1;
% pro tip
% If n is equal to number of elements in v then index will always be 1 and summa = sum(v(ii:jj))
elseif n==numel(v)
summa = sum(v(ii:jj));
index = 1;
else
% while loop for calculating the sum of n elements of subsequence of v
while jj<=numel(v)
% If sum of n elements of subsequence of v is greater than the previous one
% then that sum is stored in summa and index of first element is stored in index
if sum(v(ii:jj))>summa
summa = sum(v(ii:jj));
index = ii;
end
if sum(v(ii:jj))<0 && summa == 0
summa = sum(v(ii:jj));
index = ii;
end
ii = ii + 1;
jj = jj + 1;
end
end

Ali Mohammadi
Ali Mohammadi 2021년 10월 21일
and this is my solution for it:
function [summa, index] = max_sum(v, n)
%creat an empty vector for cunsecutive sums, and ultimately determine the
%highest value and its index with max(w)
w = [];
ii = 1;
if n > length(v)
summa = 0
index = -1
return;
else
while n <= length (v)
w(ii) = sum(v(ii:n));
ii = ii + 1;
n = n + 1;
end
[summa, index] = max(w)
end
%I have checked, it works :)

Eric
Eric 2020년 7월 22일
편집: Eric 2020년 7월 22일
I came up with a function that, I think, accomplishes this task. It runs fine in my MATLAB (2020a). However, it doesn't seem to be acceptable in the Assignment Submission. Anyone know why this could be the case?
function[summa,index]=max_sum(v,n)
if n>length(v)
index=-1;
summa=0;
else
sumn=movsum(v,n);
[summa,index]=max(sumn);
index=index-1;
end
end
  댓글 수: 5
Eric
Eric 2020년 7월 23일
Sorry, not logical indexing. That would be "normal" indexing, right? [summa,index]=max(sumn) gives the maximum and index of that (middle) value in the vector sumn. The next line changes that index to the first value in that rolling sum. When running the code with my debugger, it seems to work with a range of values.
TANUJA GUPTA
TANUJA GUPTA 2020년 7월 29일
@Eric, Actually when we type for example [summa,index] = max(sum); it gives the maximum value which is passed to summa, but in index it passes the index of the first no.
[Y,I] = max(X) returns the indices of the maximum values in vector I.
If the values along the first non-singleton dimension contain more
than one maximal element, the index of the first one is returned.

댓글을 달려면 로그인하십시오.


Capulus_love
Capulus_love 2020년 8월 11일
편집: Capulus_love 2020년 8월 11일
%why second problem doesn't run?
function [summa,index] = max_sum(v,n)
s = size(v); b = 0; c = 0; index = 0;
if s(2) < n
index = s(2) - n;
summa = 0;
return
end
for i = 1:s(2)-n+1
c = sum(v(i:i+n-1));
if c > b
b = c;
index = i;
else
b = b;
end
end
summa = b;
end
%Variable ind has an incorrect value.
% max_sum([ -66 31 61 60 -70 7 9 0 8 90 -54 72 12 ...
% 89 -50 4 86 -3 83 -52 ], 23) returned sum = 0 and index = -3 which is incorrect...
  댓글 수: 3
Capulus_love
Capulus_love 2020년 8월 11일
in my matlab program,, it returns
summa =0
index = -3
with upper problem.
I'm not sure which part is not solved.
Did I not understand the problem?
Rik
Rik 2020년 8월 11일
Put a breakpoint on the first line. Then use the debugger to execute your function line by line. When do you see the index go negative? When do your variable get unexpected values?
When you do that you will notice that your assumption of a column vector input isn't enforced anywhere.

댓글을 달려면 로그인하십시오.


LEE MUN LING
LEE MUN LING 2020년 12월 18일
function [m k]=max_sum(v,n)
m=0;
if n>length(v)
k=-1;
m=0;
return
else
for k=1:((length(v)-n)+1);
while m<max(sum(v(k:((n-1)+k))));
m=max(sum(v(k:((n-1)+k))));
m=m;
k=k;
end
end
end
  댓글 수: 2
LEE MUN LING
LEE MUN LING 2020년 12월 18일
Can you guys help me check for this,
I try several time inserting the break statement in the loop but the iteration still continues, it is suppose to stop at k=4 when my input argument is ([1 2 3 4 5 4 3 2 1],3)
Rik
Rik 2020년 12월 18일
m=m; and k=k; will not do anything, so why are they in your code? Also, you posted this as an answer, but it is a question. (and you forgot to format your code as code)
Are you aware that break will only stop one loop, not all? And have you looked at the other solutions in this thread?

댓글을 달려면 로그인하십시오.


Rahil Ginwala
Rahil Ginwala 2020년 12월 23일
function [summa, index] = max_sum(v,n)
summa = 0;
i = 0;
j = v;
m = [];
c = 1;
if n>v
summa=0;
index=-1;
else
while i < n
c = numel(v(v==max(j))); % number of times the element is in the array
summa = summa + max(j)*c; % multiplying by c
b = find(v==max(j)); % find the max elemnets of v
m = [m b];% array of positions with max element
j = j(j<max(j));% remove the max number from array
i = i + 1;
end
t = sort(m);
index = t(1,1)
end
% I am not able to stop i from goigng to value 3 which gives the error for sum and index

Abhishek Sharma
Abhishek Sharma 2021년 1월 28일
% tbh, I used some help from these forum answers too
function [summa,index]=max_sum(v,n)
% first checking whether the size of row vector v is less than n or not
% if yes then print according to the given statement
if n>length(v);
summa=0;
index=-1;
else
% so you are thinking about why i took (-n+1) right?
% think of a value of size of row vector v and n
% you will get to know the answer by yourself ,example take size of v= 10 & n=10
for i=1:(length(v)-n+1);
mysum(i)=sum(v(i:i+n-1));
end
summa=max(mysum);
% I couldn't find this one, hence i used the help of this forum
x=find(mysum==summa);
index=min(x);
end

Daniel Anaya
Daniel Anaya 2021년 4월 9일
%i dont know what to do. my works pretty good with positive integers. I dont know wht it has errors with negative integers
function [summa, index] = max_sum(v,n)
summa = 0;
ii = 1;
jj = n;
total = [];
if n > numel(v)
summa = 0;
index = -1;
elseif n == numel(v)
summa = sum(v(ii:jj));
index = v(1,1);
elseif n < numel(v)
for ii = 1:numel(v)
jj = ii + (n - 1);
if jj <= numel(v);
total = [total, sum(v(ii:jj))];
end
end
summa = max(total);
x = find(total == summa);
index2 = min(x);
index = v(index2);
end
  댓글 수: 3
Daniel Anaya
Daniel Anaya 2021년 4월 9일
편집: DGM 2023년 1월 22일
Thanks for the heads up. I already have the answer. This code works fine now.
function [summa, index] = max_sum(v,n)
summa = 0;
ii = 1;
jj = n;
total = [];
if n > numel(v)
summa = 0;
index = -1;
elseif n == numel(v)
summa = sum(v(ii:jj));
index = 1;
elseif n < numel(v)
for ii = 1:numel(v)
jj = ii + (n - 1);
if jj <= numel(v);
total = [total, sum(v(ii:jj))];
end
end
summa = max(total);
x = find(total == summa);
index = min(x);
end
Jasmine
Jasmine 2023년 1월 18일
편집: Jasmine 2023년 1월 18일
it worked for me thank you
i.e the reply above me

댓글을 달려면 로그인하십시오.


Shun Yan
Shun Yan 2021년 4월 16일
function [a,b]=max_sum(A,B)
n=1;
C=0;
for ii = 1:(size(A,2)-B+1)
C(n)=sum(A(n:(B+n-1)));
n=n+1;
end
if B>size(A,2)
a=0;
b=-1;
else
a=max(C);
b=find(C==max(C));
end
I don't get what is variable ind, and how's sum 4 index9 wrong; can someone pls help? Thanks!
  댓글 수: 2
Rik
Rik 2021년 4월 16일
What happens when you try this input to your function?
[a,b]=max_sum([1 2 3 4 5 4 3 2 1],2)
a = 9
b = 1×2
4 5
Is that correct? How can the index (the second output) be multiple values?
function [a,b]=max_sum(A,B)
n=1;
C=0;
for ii = 1:(size(A,2)-B+1)
C(n)=sum(A(n:(B+n-1)));
n=n+1;
end
if B>size(A,2)
a=0;
b=-1;
else
a=max(C);
b=find(C==max(C));
end
end

댓글을 달려면 로그인하십시오.


Giancarlo milon
Giancarlo milon 2021년 6월 29일
This is my answer
function [summa,index] = max_sum(v,n)
b=1;
summa = -1000000;
%i start it with -1000000 because you ALWAYS want to overwrite your summa
index = 0;
if n > size(v)
summa = 0;
index = -1;
return
else
%this if checks for size condition if n is bigger than the size summa is 0
%and index -1
while n<=length(v);
%this will break when n = to lenght of the vector
if sum(v(b:n)) > summa
%this condition sets the sumatory of the position from b to n.
%In this case b starts in 1 and n is the input, if the sum is
%bigger than the summa it overwrites it, thats why we wanted
%always to overwrite the value summa and we started it with
%-1000000
summa = sum(v(b:n));
index = b;
end
b = b+1;
n = n+1;
%adds +1 to b and n so my positions go from 1:n to 2:n+1 to 3:n+2
%till n = the lenght and it stops
end
end
  댓글 수: 3
Giancarlo milon
Giancarlo milon 2021년 7월 23일
the variable double has a max min number, idc wich is it. But u can change the -1000000 for that max min number that the memory can hold in the variable if u want. that should solve it. if ur input is that max min number u can add a counter of times the summa it equal to the starting number, save that position and return that position
Rik
Rik 2021년 7월 23일
Your function should handle such a case. If the user needs to know how your function works and might need to edit your function, then it isn't working well.
Such limitations should be mentioned in the documentation of your function (which it currently lacks).
If you want to overwrite a variable with the lowest possible value, you should replace it with -inf.
%the number you were looking for is this:
-realmax
ans = -1.7977e+308

댓글을 달려면 로그인하십시오.


Ujwal Dhakal
Ujwal Dhakal 2022년 1월 6일
function [summa, index] = max_sum(v,n)
a=length(v);
sumvar=zeros(1,a-(n-1)); %preallocating a vector (you can skip this for now_go ahead and read the rest of the code)
%for invalid input
if n>a
summa=0;
index=-1;
return;
end
%assume a matrix on your own and try running i and j using the formula below, you'll get it
for i=1:(a-(n-1))
sumtemp=0;
for j=i:(n+i-1)
sumtemp=sumtemp+v(j);
end
sumvar(i)=sumtemp;%takes the sum and catenates into a vector sumvar _ that was preallocated to save some computational time
end
[summa,index_temp]=max(sumvar(:)); % if you give two output arguments to a max function, the second argument returns index of that element
index=index_temp; % the index of the maximum number in the vector sumvar happens to be also the index of our first number in the sequence
end

Yifan He
Yifan He 2022년 7월 27일
function [summa,index] = max_sum(v,n)
if n > length(v)
summa = 0;
index = -1;
end
if n <= length(v)
summa = sum(v(1:n));
index = 1;
for i = 1:length(v)-n
if sum(v(i+1:i+n)) > summa
summa = sum(v(i+1:i+n));
index = i+1;
else
continue;
end
end
end

Muhammad
Muhammad 2022년 7월 30일
function [summa,index]=max_sum(v,n)
Z=length(v);
summ=(1:Z-n+1);
if n>Z
summa=0;
index=-1;
return;
else
for i=1:(Z-n+1)
summ(i)=sum(v(i:(i+n-1)));
end
end
[summa,index]=max(summ);

Nyeche
Nyeche 2022년 10월 23일
function [summa, index] = max_sum (v, n)
if n < 0 || n ~= fix(n) || ~isrow(v)
error('Input must be an integer');
elseif n > length(v)
summa = 0;
index = -1;
end
total = 0;
l = length(v);
for i = 1:l-(n-1)
total(i) = sum(v(i:i+(n-1)));
summa = max(total);
index_1 = find(total == summa);
index = min(index_1);
end

Boran Yigit Usta
Boran Yigit Usta 2022년 11월 16일
function [summa,index] = max_sum(v, n)
b = zeros(1,n);
a = find(v == max(v));
for ii = 1:n
b(ii) = max(max(v));
if find(v==b(ii),1) < a
a = find(v==b(ii),1);
end
v(find(v==max(v),1)) = 0;
end
if n > length(v)
summa = 0;
index = -1;
else
summa = sum(b);
index = a;
end
  댓글 수: 1
Boran Yigit Usta
Boran Yigit Usta 2022년 11월 16일
i tested it it with variety of inputs and it works but still i get this error;
Assessment result: incorrectrandom vectors
Variable summa has an incorrect value. max_sum([ 53 -40 60 -76 -18 31 -61 67 60 75 -60 -2 60 51 -54 -10 -4 -48 -39 80 90 96 53 88 ], 5) returned sum = 429 and index = 10 which is incorrect...

댓글을 달려면 로그인하십시오.


昱安 朱
昱安 朱 2023년 3월 5일
편집: DGM 2023년 3월 5일
function [summa,index]=max_sum(v,n)
len=length(v); %v的長度
if(n>length(v)) %n大於v的長度
summa=0;
index=-1;
return;
else
temp_sum=zeros(1,len-n+1);
for x=1:len-n+1
temp_sum(x)=sum(v(x:x+n-1));
end
[summa,index]=max(temp_sum);
end
end % end of function
  댓글 수: 1
DGM
DGM 2023년 3월 5일
편집: DGM 2023년 3월 5일
Still very samey ...
But I'll grant you that you've made some minor improvements, including the addition of a few comments. Next try adding formatting.
If you want to post answers, it's probably best to start with a thread that still has room for unique answers.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by