Ordering the closest points to a certain point
Ältere Kommentare anzeigen
I have a randomly generated set of k points on a graph. I'm trying to order each points distance in reference to the other. Each point is labelled 1 to n with no particular order. I want each of the n points to create a list that has the x and y coordinates of the other points all in increasing order of distance. Here's what I tried so far but it's not working for me.
for n=1:k
x(1,n) = 50.*rand(1,1)+25;
y(1,n) = 50.*rand(1,1)+25;
end
for n=1:k;
for i=1:k;
if n~=i
d(n,i)= sqrt(((x(1,i)-x(1,n))^2)+(y(1,i)-y(1,n))^2);
else
d(n,i)=NaN
end
end
sort(d(n,:));
end
for n=1:k;
for i=1:k;
for j=1:k;
if (sqrt(((x(1,i)-x(1,n))^2)+(y(1,i)-y(1,n))^2)) == d(n,j)
x(n,j)=x(1,i);
y(n,j)=y(1,i);
end
end
end
end
So the idea is that x(n,1) would be the x coordinate of the closest point to the nth point.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!