How to speed up my code and improve/avoid for loop
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dear all, that's my piece of code that works correctly but too slow.
T=rand(100000,1)-0.3;
eTG=zeros(size(T)); %set eTG(1)=0 and preallocate
for i=2:length(T)
eTG(i)=eTG(i-1)+T(i-1);
eTG(i(eTG(i)>0))=0;
end
Preallocation doesn't help and "run and time" tool shows that string
eTG(i(eTG(i)>0))=0;
eats 83% of the total time. Please give me your advices how to optimize this code. Are there any ways to vectorize this code and avoid loops at all?
5 comentarios
Walter Roberson
el 12 de Oct. de 2017
Dang, I got it wrong this time too. Yes, min(0, eTG(i)) if you want negative untouched and positive set to 0.
Respuestas (1)
Sergei Zhuravlev
el 12 de Oct. de 2017
Editada: Sergei Zhuravlev
el 12 de Oct. de 2017
0 comentarios
Ver también
Categorías
Más información sobre Parallel Computing Toolbox en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!