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?
No products are associated with this question.
If Gsum is sparse, there may be better ways than the following fft-based method:
numBlocks=10; M=14; N=length(Gsum); K=1200;
f=@(t) mod(t-1,N)+1; shifts = f(1:numBlocks*M:numBlocks*M*(K-1)+1); comb=accumarray(shifts.',1,[N,1]).'; Havg=ifft(fft(Gsum).*fft(comb),'symmetric');
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.
Play games and win prizes!Learn more