how to improve loops in matlab, especially with big variables.
Show older comments
hei, i have two loops, where each point is checked how many other points are within a certain radius.
the point is, that i have a huge point cloud. 100000 xy coordinates are one of the smaller files. - this makes is run for ever.
radius=1;
data=rand(100000,3);
k=size(data,1);
data(:,4)=0;
for i = 1:k
CR=[data(i,1:2) radius];
for j=1:k
P=data(j,1:2);
if isPointInCircle(P, CR)
data(i,4)=data(i,4)+1;
end
end
end
Answers (2)
Chetan Rawal
on 14 Sep 2013
0 votes
You may want to see the vectorization link here. I tried doing the vectorization for you, but just got confused in the loops. Since you understand your program better than I do, you might be able to find a way after reading the info in this link.
Image Analyst
on 14 Sep 2013
0 votes
Allocate the 4th column of data in advance of the loops. Then, data has 3 (or 4) columns so what do you think you're getting for CR and P when you refer to data(i,1:2)? What's the third index? Why are you not specifying it?
5 Comments
Markus
on 14 Sep 2013
Image Analyst
on 14 Sep 2013
Edited: Image Analyst
on 14 Sep 2013
Right - nevermind - I was thinking 3D in my head, but you have a 2D matrix. Have you tried the run and time button? I don't have isPointInCircle() so I can't really help a lot.
Image Analyst
on 14 Sep 2013
You mgiht try pulling the CR and P out of the loops:
CR=data(:,1:2);
CR(:,3) = radius;
P=data(:,1:2);
for i = 1:k
ci = CR(i,:);
for j=1:k
pj = P(j,:);
% if isPointInCircle(pj, ci)
% data(i,4)=data(i,4)+1;
% end
end
end
Markus
on 14 Sep 2013
Markus
on 14 Sep 2013
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!