Increasing the caclulation speed while using loops
Show older comments
Dear MATLAB experts,
I considered a range for x and y and if x1 and y1 are within a specific range the code must convert x1 and y1 to the mean value of the specific range.
In other words, considering a pixel and if the values of x and y are inside the pixel, I wanted to replace the values with the position of the center of the pixel as shown in the attached file (if x and y are black corss must be replaced with red one)

Code is working but as the data is very big, the calculation speed is slow. Is there any modifications that may lead to increase the speed of calculations?
Thank you in advance.
data = load('data.mat').out1_com;
det_x = 50;
det_y = 50;
det_ch = 16;
%%
xedges = -(det_x/2):(det_x/det_ch):(det_x/2);
yedges = -(det_y/2):(det_y/det_ch):(det_y/2);
kf = data(:,1);
x1 = data(:,2);
y1 = data(:,3);
z1 = data(:,4);
len = length(x1);
x11 = zeros(len,1);
y11 = zeros(len,1);
for k=1:length(x1)
for i = 1:length(xedges)-1
for j = 1:length(yedges)-1
idx1 = (xedges(i) <= x1(k)) & ((x1(k)) < xedges(i+1)) & (yedges(j) <= (y1(k))) & ((y1(k)) < yedges(j+1));
if (idx1 == 1)
x11(k)=(xedges(i)+xedges(i+1)) ./ 2;
y11(k)=(yedges(j)+yedges(j+1)) ./ 2;
end
end
end
end
pos=[kf,x11,y11,z1];
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!