2 views (last 30 days)

The section of code below works to count how many times a particular grid point (with grid points much smaller than the circles themselves) is contained within a circle (i.e. how many circles overlap at each grid point) by making a mask for each individual circle and then adding the total to a matrix "cnts". My problem is that I need to do this for potentially hundreds of thousands of circles. Is there a way to speed up the masking/counting process without using the for loop (which I'm assuming is taking most of the time)? Thanks for any suggestions!

---------------------------------------------------------------------

%% loop over each circle to create a mask and count how many circles hit each grid point

cnts = zeros(size(xx));

for i = 1:length(xc)

xc = x_centers(i); % find the center point of circle #i

yc = y_centers(i);

mask = ((xx-xc).^2 + (yy-yc).^2)<(beam_diameter/2)^2; %create a mask for that circle

% add counts to a grid to determine how many circles hit each grid point

cnts = cnts+mask;

end

Ganesh Regoti
on 17 Jul 2019

From your question, you need an alternative which works as for loop and more optimized. The concept of vectorization works well for your case.

You can refer more about vectorization here

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.