フィルターのクリア

speed up the code

1 回表示 (過去 30 日間)
Tiki Tiki
Tiki Tiki 2018 年 7 月 23 日
コメント済み: Jan 2018 年 8 月 3 日
hi everyone.
can you help me speed up this code?
tic
InputOverlap = magic(64)
SDR_Overlap = InputOverlap;
SDR = (zeros (64,64)) ;
Radius = 2;
InputOverlap = [InputOverlap(:,1:Radius) InputOverlap InputOverlap(:,end+1-Radius:end)];
InputOverlap = [InputOverlap(1:Radius,:) ; InputOverlap ; InputOverlap(end+1-Radius:end,:) ];
for r=1:64
for c=1:64
Neighbour= InputOverlap(r:r+2*Radius,c:c+2*Radius);
Kmax = max(Neighbour(:)) ;
if (SDR_Overlap(r,c)>0)&(SDR_Overlap(r,c)>= Kmax)
SDR(r,c) = 1;
else
SDR(r,c) = 0;
end
end
end
toc
Thanks.
  2 件のコメント
Jan
Jan 2018 年 7 月 23 日
編集済み: Jan 2018 年 7 月 23 日
Start with omitting:
else
SDR(r,c) = 0;
SDR is initialized to zero already.
The editor should show a hint that && is more efficient than &. Consider these MLint messages.
The main part of your code happens before the loop. Most of all displaying the magic matrix is slow. I guess, you want to measure the time inside the loop only, don't you?
Tiki Tiki
Tiki Tiki 2018 年 7 月 26 日
Yes. My problem is time in the loop. I remove SDR(r,c) = 0 by setting it is zeros before loop.
But time consumes still high. How can I remove loop in this case?
Please help me. Thank.

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

採用された回答

Jan
Jan 2018 年 7 月 23 日
This is slightly faster:
tic
for r = 1:64
for c = 1:64
Neighbour = InputOverlap(r:r+2*Radius, c:c+2*Radius);
Kmax = max(Neighbour(:));
SDR(r,c) = (SDR_Overlap(r,c) >= Kmax);
end
end
toc
Is the SDR_Overlap(r,c)>0 test useful?
  2 件のコメント
Tiki Tiki
Tiki Tiki 2018 年 7 月 26 日
Yes. it is a little faster. i also remove SDR_Overlap(r,c)>0. but this code still consume much time.
so i need optimze more. Can you help me how to remove loop in this case?
I have gpu. but dont undertand to use it.
Jan
Jan 2018 年 8 月 3 日
Use movmax to replace the loops.
Does the padding of the input matrix belong to the problem? With movmax and 'EndPoints' set to 'shrink' you can omit the padding.
can you post some real input data? Especially the dimensions matter.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by