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

2 comentarios

Matt J
Matt J el 26 de Oct. de 2016
Editada: Matt J el 26 de Oct. de 2016
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 el 20 de En. de 2017
(Answers Dev) Restored Question.

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 26 de Oct. de 2016
Editada: James Tursa el 26 de Oct. de 2016

0 votos

cumavg = cumsum(v)./(1:numel(v));
mark = find(cumavg<=(avg-0.01),1,'last');

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

nc
el 26 de Oct. de 2016

Comentada:

el 20 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by