Info

This question is closed. Reopen it to edit or answer.

Is it possible to reduce implementation time of this code?

1 view (last 30 days)
This code operates on a matrix, MAT . To save you the pain of reading through many lines of code, I defined the top 5 variables for your convenience. As you will see in the code, I have many disks. Each disk has center coordinates given by disks_center_coordinates . Each disk has a radius given by disks_radii.
Now, what I am trying to do is to blur some patches in MAT. These patches, as you may have guessed, are the disks. After each time I blur a patch, I will take the sum of all elements of the blurred patch, and store this scalar value in a matrix, A .
The problem I am having is that this code takes a lot of time (sometimes, as much as 30 minutes). I am wondering if there is some way to reduce implementation time. Your help is highly appreciated.
MAT = [some K-by-L matrix] ;
disks_center_coordinates = [2-by-N matrix] ;
disks_radii = [1-by-N matrix] ;
M = some scalar ;
N = some scalar ;
A = zeros(M,N) ; %initialization
[x, y] = meshgrid(1:size(MAT,2), 1:size(MAT,1));
for j=1:N
C = (x-disks_center_coordinates(1,j)).^2+...
(y-disks_center_coordinates(2,j)).^2 <= disks_radii(j)^2;
A(1,j) = sum(sum(MAT(C)) ;
end
for i=2:M
gaussian_blur_mask = fspecial('gaussian',3,SIGMA(i-1)) ;
for j=1:N
C = (x-disks_center_coordinates(1,j)).^2+...
(y-disks_center_coordinates(2,j)).^2 <= disks_radii(j)^2;
TEMP = filter2(MAT(C),gaussian_blur_mask) ;
A(i,j) = sum(sum((TEMP)) ;
end
end
  2 Comments
Geoff Hayes
Geoff Hayes on 2 Aug 2015
Pyroholic - try using profile to see where the bottlenecks are in your code which may give you an idea of what to improve.
Alfonso Nieto-Castanon
Alfonso Nieto-Castanon on 2 Aug 2015
before speeding the code you probably want to make sure it does what you want, and I really doubt that in the current state it does. First MAT(C) is a vector so it really does not make sense to use filter2 on it. Second why would you want to filter that if all you are interested in is the sum of the filtered values? (there are considerably better ways to get that sum without actually filtering the data). Perhaps you could clarify what you are trying to accomplish with this computation?

Answers (1)

Jan
Jan on 2 Aug 2015
Edited: Jan on 2 Aug 2015
Avoid repeated calculations:
  • disks_radii(j)^2 is calculated M times. So better store the result in a temporary variable.
  • As far as I can see, MAT(C) is a vector already, then a single sum is enough.
  • C is calculated M times. Changing the order of the loops might be useful, if fspecial does not consume more time.
  • The call of filter2 can be accelerated, if you call conv2 directly. Look in the source of filter2.m to learn more.
Finally I do not think, that these suggestion will decrease the processing time substantially, because I guess that the main time is spent in conv2. Use the profiler to find the bottleneck of you code.

Community Treasure Hunt

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

Start Hunting!