how to vectorize this loop
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
nc
am 26 Okt. 2016
Kommentiert: Rena Berman
am 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 Kommentare
Matt J
am 26 Okt. 2016
Bearbeitet: Matt J
am 26 Okt. 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
Akzeptierte Antwort
James Tursa
am 26 Okt. 2016
Bearbeitet: James Tursa
am 26 Okt. 2016
cumavg = cumsum(v)./(1:numel(v));
mark = find(cumavg<=(avg-0.01),1,'last');
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!