How to increase speed of a multi-vector calculation

조회 수: 3 (최근 30일)
moh mor
moh mor 2023년 12월 18일
댓글: moh mor 2023년 12월 18일
Hello,
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);
end
and:
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;
end
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

답변 (1개)

Rik
Rik 2023년 12월 18일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by