how to improve loops in matlab, especially with big variables.
Afficher commentaires plus anciens
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
Réponses (2)
Chetan Rawal
le 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
le 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 commentaires
Markus
le 14 Sep 2013
Image Analyst
le 14 Sep 2013
Modifié(e) : Image Analyst
le 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
le 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
le 14 Sep 2013
Markus
le 14 Sep 2013
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!