how to improve loops in matlab, especially with big variables.
Ältere Kommentare anzeigen
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
Antworten (2)
Chetan Rawal
am 14 Sep. 2013
0 Stimmen
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
am 14 Sep. 2013
0 Stimmen
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 Kommentare
Markus
am 14 Sep. 2013
Image Analyst
am 14 Sep. 2013
Bearbeitet: Image Analyst
am 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
am 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
am 14 Sep. 2013
Markus
am 14 Sep. 2013
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!