Speed up a for loop in my programme?
1 view (last 30 days)
Hi, one of the part of my programme contains this piece of code:
A(k+l-1,j+h-1) = A(k+l-1,j+h-1) *exp(+1i*X); %phase in the square gran x gran
My pc runs this code in 0.60 seconds but I would like to know if it is possible to speed up this process.
I have already looked similar questions like:
but I don't know if I can apply to my problem
Eike Blechschmidt on 30 Jul 2021
If I understood you right this could do the trick and is about 2.5x faster on my machine.
[row,col] = ind2sub([size2, size2],1:size2*size2);
blockRow = ceil(row/gran);
blockCol = ceil(col/gran);
idx = sub2ind([size2/gran, size2/gran], blockRow, blockCol);
A = ones(size2, size2);
A(1:size2*size2) = exp(+1i*X(idx));
Important to not is that size2 needs to be a multiple of gran to for this solution to work.