how to improve loops in matlab, especially with big variables.

1 visualización (últimos 30 días)
Markus
Markus el 14 de Sept. de 2013
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
  2 comentarios
Markus
Markus el 14 de Sept. de 2013
in the meantime i installed the kd-tree.
however, i dont know how i can implement the isPointInCircle function or the loop into it.
Markus
Markus el 14 de Sept. de 2013
the run and time analyses showed, that by 1000 datapoints the function isPointInCircle http://www.mathworks.com/matlabcentral/fileexchange/7844-geom2d/content/geom2d/geom2d/isPointInCircle.m needs about 20 seconds, while the loops need only 6 seconds. therefore is it more essential, to find a way to avoid unnecessary comparisons.

Iniciar sesión para comentar.

Respuestas (2)

Chetan Rawal
Chetan Rawal el 14 de Sept. de 2013
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.
  2 comentarios
Markus
Markus el 14 de Sept. de 2013
hei, thanks for this answer. i am not sure how this works, since i dont calculate one value.
the first loop (i). chooses the point (CR) with the radius. the second loop checks every point (P) whether it is within this circle or not. if yes, the counter goes one up.
Markus
Markus el 14 de Sept. de 2013
ill try to explain the loops: loop 1 (i = 1:k), defines the point in the point cloud with a certain radius. and each point in the point cloud has to be compared to all others, whether the point is within the distance or not.
loop 2 (j = 1 : k), chooses one point (P) after another and checks whether this point lies within the radius of the first point (CR) if yes, the counter goes up.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 14 de Sept. de 2013
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 comentarios
Markus
Markus el 14 de Sept. de 2013
i did the run and time (guess our posts did overlap) :) : the run and time analyses showed, that by 1000 datapoints the function isPointInCircle
http://www.mathworks.com/matlabcentral/fileexchange/7844-geom2d/content/geom2d/geom2d/isPointInCircle.m
needs about 20 seconds, while the loops need only 6 seconds. therefore is it more essential, to find a way to avoid unnecessary comparisons.
i am gonna try your idea and let you know the results.
Markus
Markus el 14 de Sept. de 2013
i tried this and it went faster. unfortunatly only one second. - the problem is, that i need to get rid of unnecessary loops. so that points wich are far out of range are not going to be tested again. or i need a method, to skip the points in the second loop, when they have been tested in the first loop.
Generated 14-Sep-2013 18:13:12 using cpu time.
Function Name Calls Total Time Self Time*Total Time Plot
cleanPointClouds 1 24.952 s 5.131 s
isPointInCircle 1000000 19.822 s 19.822 s

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by