How to increase speed of a multi-vector calculation

3 views (last 30 days)
I have implemented a loop like this:
for coorC = 1:numel(y1f),
AF_Nunir(:,coorC) = AF_surff( x1f.', y1f.', x1f(coorC), y1f(coorC), (Xr.')*0.015, (Yr.')*0.015 ,K);
function z = AF_surff(u , v, u0 , v0 , X, Y , K )
distance = (sqrt((X-u0).^2 + (Y-v0).^2 + 0.4^2) - sqrt((X - u).^2 + (Y - v).^2 + 0.4^2));
AF_out1 = sum(exp(1j*(K.').*(distance(:).')),1);
AF_out2 = sum(reshape(AF_out1,[numel(X),numel(u)]),1);
z = AF_out2;
where "K", "Xr", "Yr", "x1f", and "y1f" are vector. I could implement this code using multiple nested "for" loop. But, I'd rather using vector computation in order to increase running speed. Above block must be rUn multiple times in our code, So it is crucial to implement this blcok efficiently. This takes 145s in a "core i9 12900K" CPU. What is your advice to perform this manipulations efficiently? I've heard about parallel computing. Does it effectively play a crucial role for solving this kind of problem?
Any help would be appreciated.
Thank you

Answers (1)

Rik on 18 Dec 2023
The iterations are not interdependent, so you should be able to use a parfor loop instead.
You could consider performing those conjugations (the .'), since those take up time every iteration.
Another speedup would be to replace distance(:).' by a call to reshape.


Find more on Loops and Conditional Statements in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!