how to vectorize this loop
2 次查看(过去 30 天)
显示 更早的评论
for i=1:length(v) %for each numer from 1 to length of arry
if(i==1) %1/1 = 1
cumavg(i)=v(i);
else
cumavg(i)=0;
for j=1:i
cumavg(i)=cumavg(i)+v(j);
end
cumavg(i)=cumavg(i)/i;
end
if(cumavg(i)<= (avg-0.01))
mark=i;
end
end
2 个评论
Matt J
2016-10-26
编辑:Matt J
2016-10-26
nc asked (and then deleted):
How to vectorize this loop
for i=1:length(v) %for each numer from 1 to length of arry
if(i==1) %1/1 = 1
cumavg(i)=v(i);
else
cumavg(i)=0;
for j=1:i
cumavg(i)=cumavg(i)+v(j);
end
cumavg(i)=cumavg(i)/i;
end
if(cumavg(i)<= (avg-0.01))
mark=i;
end
end
采纳的回答
James Tursa
2016-10-26
编辑:James Tursa
2016-10-26
cumavg = cumsum(v)./(1:numel(v));
mark = find(cumavg<=(avg-0.01),1,'last');
0 个评论
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!