speed up the code
1 次查看(过去 30 天)
显示 更早的评论
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
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?
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!