Reputation: 247
I have a grid with fixed points and random generated user's positions.
Distances for each point and user are measured from the beginning of the axis 0.0. I want to associate each user to the closest fixed point. I calculate both distance vectors and the min of them per user is pointing to the closest fixed point.
But i am stuck on finding a working way so each fixed point and associated user have something same in plot, p.e. same color and color area.
So my problem is two dimensional:
Thank you.
Upvotes: 1
Views: 299
Reputation: 3303
For the point searching I would use dsearchn
for this kind of thing. You can use it with or without delaunay triangulation depending on the ratio of users to fixed sites. I tend to use it the quick and easy way, which in your case would be:
indices_of_closest_fixed_points = dsearchn(fixed_points, user_points)
As for the colors I would suggest you define a color map using something like
mymap = lines(n)
where n
is the number of fixed points you have. You can then use scatter
to plot the points with specific colors and sizes. Perhaps something like this to get you started:
x = user_points(1,:);
y = user_points(2,:);
S = []; % point sizes, left empty for now
C = mymap(indices_of_closest_fixed_points,:); %colors
scatter(x,y,S,C);
Upvotes: 1
Reputation: 20319
To find the nearest point simply compute the euclidean distance between each user
point and the complete set of fixed
points. Then the index of the shortest distance will also be the index of the fixed
point.
dist = calc_dist(fixedPts, aSingleUserPt)
[~, idx] = min(dist);
To solve the color problem, you'll need to create a colormap from a fixed
point index to a unique color. Then when you plot
a user point you will set the color of the plot equal to the colormap evaluated at idx
Note the euclidean distance is very easy to calcuate:
euc_dist = sqrt( (x1 - x2)^2 + (y1 - y2)^2 );
There are functions on File Exchange that will let you compute this quickly.
Upvotes: 0