finding closer points to a given co-ordinates

Hi, everyone: I have two centers P(x1,y1) and Q(x2,y2). In each coordinate there are cluster of 10 points normally distributed with standard deviation 2,and mean 0. How to write code in matlab to find closer points around the P and Q and compare? If anybody have idea please share. Thank you.
Regards; SGiri

Answers (3)

Try this:
% Find point in x1 and y1 vectors that is closest to point P with coordinates (Px, Py).
distancesP = sqrt((x1 - Px).^2 + (y1 - Py) .^ 2);
[minDistanceP, indexOfMinP] = min(distancesP);
% Find point in x2 and y2 vectors that is closest to point Q with coordinates (Qx, Qy).
distancesQ = sqrt((x2 - Qx).^2 + (y2 - Qy) .^ 2);
[minDistanceQ, indexOfMinQ] = min(distancesQ);

8 Comments

thanks for your kind cooperation. I think you did not understand what i mean. I want closer points around the center P among the cluster of 10 points normally distributed with mean 0 and standard deviation 2. 'minDistand' command gives only single points which is not my need.
I still don't know what you mean. Let's say you have point (Px,Py) and 10 other points randomly located around that point. This cluster of 10 other points will have a centroid, which will probably not be at exactly (Px, Py), and they will have 10 distances from their centroid and those 10 distances will have a standard deviation, which probably won't equal exactly 2.
But for the sake of argument, let's pretend that the centroid of the 10 other points was at (Px, Py) and that the 10 points had a standard deviation of distances from the centroid of 2. OK, unlikely but fine. You say "I want closer points around the center P" -- what exactly does this mean? What are the closer points? And which ones are NOT closer? Those closer than one standard deviation? Some other criteria? I have no idea. Please explain your criteria for specifying which points are "closer" and "not closer."
Please attach a .mat file with your points in the cluster attached. And tell us which are the points P and Q, and if they're in the cluster of 10 points or if they are separate variables.
here is the problem: Consider the points P(5,10) and Q(10,5) in R^2. Use Matlab to create 50 points equally divided into two clusters in R^2, first one centered at P, and the second one centered at Q.Make the distances of the points from the centers normally distributed with a mean 0 and standard deviation 2. Use these numbers as indices to select 20 points from the total of 50 points. Compute in Matlab, out of the 20 points selected, how many are closer to point P than point Q. That's it.
That is the question given to me for solution. When I plotted the two clusters there are many points seen closer to P and Q. But I dond know how to count these points using matlab code.
See if this is what you want. Note: If it's your homework to turn in, then you can't ethically turn in my work as your own.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Make 25 points around P
N = 25;
distances = 2 * randn(1, N);
angles = 360 * rand(1, N);
P = [5, 10]; % [x, y]
xP = P(1) + distances .* cosd(angles);
yP = P(2) + distances .* sind(angles);
% Plot random points.
plot(xP, yP, 'b.', 'MarkerSize', 20);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
hold on;
% Plot point P.
plot(P(1), P(2), 'r*', 'MarkerSize', 18, 'LineWidth', 2);
grid on;
% Plot a circle at a radius of 2
radius = 2;
diameter = 2 * radius;
pos = [P(1)-radius, P(2)-radius, diameter, diameter];
rectangle('Position',pos,'Curvature',[1 1], 'LineWidth', 2, 'EdgeColor', 'm')
axis equal;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Repeat again for another 25 points around point Q
Q = [10, 5]; % [x, y]
distances = 2 * randn(1, N);
angles = 360 * rand(1, N);
xQ = Q(1) + distances .* cosd(angles);
yQ = Q(2) + distances .* sind(angles);
% Plot random points.
plot(xQ, yQ, 'b.', 'MarkerSize', 20);
hold on;
% Plot point P.
plot(Q(1), Q(2), 'r*', 'MarkerSize', 18, 'LineWidth', 2);
grid on;
% Plot a circle at a radius of 2
radius = 2;
diameter = 2 * radius;
pos = [Q(1)-radius, Q(2)-radius, diameter, diameter];
rectangle('Position',pos,'Curvature',[1 1], 'LineWidth', 2, 'EdgeColor', 'm')
% Combine sets
x = [xP, xQ];
y = [yP, yQ];
% Take 20 at random
indexesToTake = randperm(length(x), 20);
xt = x(indexesToTake);
yt = y(indexesToTake);
% Plot those with a circle around them.
plot(xt, yt, 'ro', 'MarkerSize', 20);
% Find distances to P
distancesToP = sqrt((xt - P(1)).^2 + (yt - P(2)).^2)
% Find distances to Q
distancesToQ = sqrt((xt - Q(1)).^2 + (yt - Q(2)).^2)
% Find indexes of those closer to P
closerToP = distancesToP < distancesToQ;
% Count them
numCloserToP = sum(closerToP)
numCloserToQ = sum(~closerToP)
I appreciate your hard work for me. This is a part of a big exercise not a homework. definitely, it could be a part of exam question. it is preparation for big project.I am trying to figure-out your style of writing code. I did all parts but I could not do closer point counting. Now I got idea. Still I would not convinced whether your answer is correct. Thank you again.
Well then perhaps it's because of the sentence "Use these numbers as indices to select 20 points from the total of 50 points." Use WHAT numbers? How exactly would the mean, standard deviation of distance, and the location of the reference points be used as indices? In what way could those numbers be used to make the selection? I couldn't see how so I just chose them randomly.
I will show you, when completed.

Sign in to comment.

If cluster 1 elemets are arranged like this
X1 = [x1 y1;
x2 y2;
....
....
x10 y10];
and P is a row vector than to find the minimum distance point use
[minValue, minIndex] = min(vecnorm(X1 - P, 2, 2));
similarly, you can find points closest to Q.
John BG
John BG on 19 May 2018
Edited: John BG on 19 May 2018
Hi Subarna Giri
On April 30th I answered the really similar question
nearest point from two matrices
My answer to that question not only finds the distance between the nearest points of 2 sets, like P and Q in your question, but also returns
1.-
a matrix Da that has all distances among all points ordered by proximity.
2.-
a matrix Na ranking the elements of both sets by proximity, it may also come handy, may it not?
Subarna, replace the following A and B with the sets P and Q of your question.
If you supply P and Q I will update my answer with the coordinates you make available.
clear all;clc;close all
N=10
Ln=[1:N]
A=randi([0 50],N,2);
B=randi([0 50],N,2);
plot(A(:,1),A(:,2),'r*');grid on
axis([0 50 0 50])
axis equal
hold on
for k=1:1:N
text(A(k,1),A(k,2),[' ' num2str(Ln(k))],'FontSize',12,'Color','red')
end
plot(B(:,1),B(:,2),'*','Color',[.2 .7 .2]);grid on
for k=1:1:N
text(B(k,1),B(k,2),[' ' num2str(Ln(k))],'FontSize',12,'Color',[.2 .7 .2])
end
.
.
Now let's define 2 matrices for the results
2.-
Da2=zeros(Na,Nb)
.
Da2 is for the distances of each element of A to all elements of B.
.
Na2=zeros(Na,Nb)
.
Na2 is for the ordered numerals according to distance, the 1st is the nearest.
for k=1:1:Na
% L1=[1:k];L1(end)=[];L1=[L1 k+1:N] % numerals of all B neighbour except kth neighbour
pa=repmat(A(k,:),Nb,1)
Da=(sum((pa-pb).^2,2)).^.5 % distance of a point to all B neighbours
D0=sortrows([Da Lnb'])
Da2(k,:)=D0(:,1) % update sorted distances
Na2(k,:)=D0(:,2) % update sorted numerals
end
3.-
The results
Na2 =
3 4 5 1 6 2
6 2 5 4 1 3
4 1 2 3 6 5
2 6 1 4 5 3
4 3 1 5 2 6
1 4 2 6 3 5
2 1 6 4 5 3
5 6 2 3 4 1
2 6 1 4 5 3
6 2 4 1 5 3
If you focus on the 1st column
Na2(:,1)
=
3
6
4
2
4
1
2
5
2
6
reading it as follows
1st element of A - GREEN, is closest to 3rd element of B - RED.
2nd element of A - GREEN, is closest to 6th element of B - RED.
3rd element of A - GREEN, is closest to 4th element of B - RED.
4th element of A - GREEN, is closest to 2nd element of B - RED.
.
Subarna
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG

Categories

Products

Release

R2018a

Tags

Asked:

on 18 May 2018

Commented:

on 19 May 2018

Community Treasure Hunt

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

Start Hunting!