How can I use the MATLAB vector processing instead of this for loop?
Afficher commentaires plus anciens
Hi guys,
does anybody have an idea how I could speed up the following loop:
Havg = zeros(1, length(Gsum));
for k=1:K
Havg = Havg + circshift(Gsum, [0 numBlocks*M*(k-1)]);
end
Here, length(Gsum)=280000 and K=1200. NumBlocks equals 10 and M=14. This loop is very slow. However, I did not find a way to use the MATLAB-specific vector processing to speed it up.
How can I speed up the loop, e.g. with MATLABs powerful vector processing?
Regards, Max
Réponse acceptée
Plus de réponses (1)
Roger Stafford
le 11 Déc 2012
As your code is now, you are performing 280,000 X 1,200 = 336,000,000 additions. You can cut down the number of flops by a factor of about 1/333 with the use of matlab's 'cumsum' function.
p = 140; q = 2000; r = 1200;
Havg = reshape(Gsum,p,q);
Havg = cumsum([zeros(p,1),Havg,Havg(:,1:r-1)],2);
Havg = reshape(Havg(:,r+1:r+q)-Havg(:,1:q),1,[]);
It should be noted, however, that the above reduction in the number of flops comes at the cost of increased round-off error accumulated by 'cumsum' over q+r = 3200 steps in each of the 140 rows in the next-to-last line.
Roger Stafford
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!