how to vectorize this loop

2 visualizzazioni (ultimi 30 giorni)
nc
nc il 26 Ott 2016
Commentato: Rena Berman il 20 Gen 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 Commenti
Matt J
Matt J il 26 Ott 2016
Modificato: Matt J il 26 Ott 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 il 20 Gen 2017
(Answers Dev) Restored Question.

Accedi per commentare.

Risposta accettata

James Tursa
James Tursa il 26 Ott 2016
Modificato: James Tursa il 26 Ott 2016
cumavg = cumsum(v)./(1:numel(v));
mark = find(cumavg<=(avg-0.01),1,'last');

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by