Effacer les filtres
Effacer les filtres

how to vectorize this loop

2 vues (au cours des 30 derniers jours)
nc
nc le 26 Oct 2016
Commenté : Rena Berman le 20 Jan 2017
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 commentaires
Matt J
Matt J le 26 Oct 2016
Modifié(e) : Matt J le 26 Oct 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 le 20 Jan 2017
(Answers Dev) Restored Question.

Connectez-vous pour commenter.

Réponse acceptée

James Tursa
James Tursa le 26 Oct 2016
Modifié(e) : James Tursa le 26 Oct 2016
cumavg = cumsum(v)./(1:numel(v));
mark = find(cumavg<=(avg-0.01),1,'last');

Plus de réponses (0)

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by