How increase calculate speed in for loop

3 ビュー (過去 30 日間)
kisik KIM
kisik KIM 2021 年 9 月 14 日
コメント済み: Jan 2021 年 9 月 15 日
Hi all.
I have a problem in my code about too long calculate time.
This is my part of code
=============================================
Depth=5000;
Num_Alines=400;
Num_Bscan=300;
Alines=180
half=Alines/2;
ReconImage=zeros(Depth,Num_Alines,Num_Bscan);
for i=1:Depth
Sampledist2=Sampledist;
Sampledist2(Sampledist2==Sampledist2(i,round(half),round(half)))=1;
Sampledist2(Sampledist2~=1)=0;
Sampledist2=Sampledist2*D313; %D313 is one data not array.
for j=round(half):Num_Alines-round(half)
for k=round(half):Num_Bscan-round(half)
ReconImage(:,1+j-round(half):j+round(half),1+k-round(half):k+round(half))=ReconImage(:,1+j-round(half):j+round(half),1+k-round(half):k+round(half))+fftimage(i,j,k).*Sampledist2/1000;
end
end
end
=============================================================================
In my code, Sampledist is (Depth,Alines,Alines) size aray.
Ultimately, I want sum "fftimage(i,j,k).*Sampledist2/1000" to "ReconImage" array. But in my code, 'for k=~' part spend 0.8 sec during one cycle. So actually, expected spend time is up to ten thousands hours.
I never agree my code is the best.
When i search to increase calculation, i found 'parfor' but i can't apply that because for loop refer the result.(underline)
Anyone have nice idea?
Thank you.
Kim.
  1 件のコメント
Jan
Jan 2021 年 9 月 14 日
What is "Sampledist"?

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2021 年 9 月 14 日
編集済み: Jan 2021 年 9 月 14 日
Start with a simplification of the code:
Depth = 5000;
Num_Alines = 400;
Num_Bscan = 300;
Alines = 180
h = round(Alines / 2); % Call round() once only
R = zeros(Depth, Num_Alines, Num_Bscan);
for i = 1:Depth
S = Sampledist;
S(S == S(i, h, h)) = 1;
S(S ~= 1) = 0;
S = S * D313 / 1000;
for j = h:Num_Alines - h
for k = h:Num_Bscan - h
R(:, 1+j-h:j+h, 1+k-h:k+h) = ...
R(:, 1+j-h:j+h, 1+k-h:k+h) + fftimage(i,j,k) .* S;
end
end
end
Please provide some values for Sampledist and fftimage - maybe produced by rand().
  3 件のコメント
kisik KIM
kisik KIM 2021 年 9 月 15 日
編集済み: kisik KIM 2021 年 9 月 15 日
Above code calculate each array but below code calculate each elementals.
for i=1:DataLength
for j=round(half):Num_Alines-round(half)
for k=round(half):Num_Alines-round(half)
for a=1:Alines
y = find( Sampledist(:,round(half),a) == Sampledist(i,round(half),a));
for l=1:numel(y)
array(l,1)=fix(y(l)/m)+1;
array(l,2)=rem(y(l),m);
if j - array(l,1) + round(half) > 0
ReconImage(i,j,k-round(half)+a) = ReconImage(i,j,k-round(half)+a) + fftimage(i+(array(l,2)-1),j+(array(l,1)-round(half)),a)*D313(array(l,2),array(l,1),a)/10000;
else
end
end
end
end
end
end
Surprisingly, This code is faster. I guess the problem is array size.(As you can see, full size of "S" array are don't needed because some of part have data(others are zero).
So, I'm not sure which one is better.
Jan
Jan 2021 年 9 月 15 日
Sorry, I tried to guess the sized of the inputs, but the code still stops with errors.
Please do not let the readers guess, what your input arguments are. If I cannot run your code, it is very hard to improve its speed.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by