how to vectorize this loop

조회 수: 9 (최근 30일)
nc
nc 2016년 10월 26일
댓글: Rena Berman 2017년 1월 20일
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
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
Rena Berman
Rena Berman 2017년 1월 20일
(Answers Dev) Restored Question.

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

채택된 답변

James Tursa
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개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by