i dont know whats the problem with this fails with variable test

1 次查看(过去 30 天)
function [summa index]=max_sum(v,n)
m=zeros(1,length(v));
x=zeros(1,length(v));
j=1;
if n>length(v)
summa=0;
index=-1;
elseif n<length(v)
for ii=1:length(v)
if ii<=length(v)-n+1
x=v(:,ii:n+ii-1);
m(ii)=sum(x);
else
x=v(:,ii:length(v));
z=v(:,1:j);
m(ii)=sum(x)+sum(z);
j=j+1;
end
end
[y i]=max(m);
summa=max(y);
index=max(i);
else
summa=sum(v);
index=1;
end
  2 个评论
Mario Malic
Mario Malic 2020-8-1
编辑:Mario Malic 2020-8-1
We also don't know what's the problem. Could you tell us?
What is this?[summa index]
Shekhda Dhrumil
Shekhda Dhrumil 2020-8-1
here v is an array,n is a number.
we have to make continuous group of n element from v.
then we need to find whose sum is highest and then we need to provide that highest sum as output in summa and the index of first element of that group in output argument index.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2020-8-1
编辑:Image Analyst 2020-8-1
When you do
m(ii)=sum(x);
x is a 2-D array. So sum(x) is a 1-D array, because it's summing each column, and you're trying to put a whole array into a single element, the ii'th element of m. What do you intend to sum? The whole array? If so, do this:
m(ii)=sum(x(:));
  6 个评论
Shekhda Dhrumil
Shekhda Dhrumil 2020-8-2
so that i can copy that particular part of that vector in other vector and then add its all element to find sum of that part.
is there any simple alternative to that problem?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by