Drawing lines from 2 or more separate points to one target point and calculating the distance between them

Hello, I have a plot where I create random points scattered around a set of central points. I am trying to draw lines from the 2 central points to another target point,. There will be more central points than 2 in the future so an adaptable solution is preferred. After drawing the lines, I need to calculate the distance between each point and the target point, find the minimum distance value and keep the line associated with the minimum distance value and delete the longer one. Below Ive included a picture of the basic situation. I line A is longer than line B then I will keep line B and vice versa. I need to be able to do this for every point on the graph and hopefully simultaneously. I also included the test code I am using to draw lines between one central point and one target but I havent been able to tweak it properly for multiple points.
for i = 1:LengthB
x2(i) = B(i,1);
y2(i) = B(i,2);
Line_matrix1(i,:) = line([x0, x2(i)],[y0, y2(i)], 'linewidth', 1.5, 'color', 'r');
drawnow;
hold on
end
CTR Demo Incomplete 3.PNG

 Réponse acceptée

Use pdist2() to compute the distance between point p and points q. Compute the minimum distance and then plot that line (no need to plot all lines to find the distances).
% define point p and points [q]
p = [2,3]; %[x,y]
q = [7,3; %[x,y]
5,4;
1,5];
% Compute distance between point p and points [q]
dist = pdist2(p,q);
% Find shortest distance; minDistIdx will be the row number in q
[minDist, minDistIdx] = min(dist);
% Plot shortest line
plot([p(1),q(minDistIdx,1)], [p(2),q(minDistIdx,2)])

11 commentaires

Hey Adam I actually need to draw the lines just so that I can show which point Im looking at when doing the distance calculation. Can I just draw two lines and then use the code you provide for the rest of the task??
There probably isn't a need to calculate each distance individually. In my answer, all distances are calculated at once. If you want to plot all of the lines that connect the list of points in 'q' to the target point "p",
plot([q(:,1),p(1)*ones(size(q,1),1)]',[q(:,2),p(2)*ones(size(q,1),1)]')
...assuming the points are arranged as they are in my answer. Let me know if anything is unclear.
also when you are using pdist2 does that mean I have to record all the points in one place under the variable q or can i use it as such dist = pdist2(p(i), q(i)); in a for loop or would that be less efficient.
Well, the point of pdist2() is to avoid the loop. If you're going to use a loop you just just use the Euclidean distance formula between points.
So yes, if you're using pdist 2, the 2nd input should be a matrix of points.
Apologies my question was a bit unclear what i meant to ask was does p have to be a single point or can it be a matrix as well? cause Im gonna have to check the distance between each vein node (black dots) and each hormone seed (red dots) and I thought that it would be easier if I was working with matrices.
The documentation is great for answering these types of questions.
From that link, for pdist2(x,y), "Input data, specified as a numeric matrix. X is an mx-by-n matrix and Y is an my-by-n matrix. Rows correspond to individual observations, and columns correspond to individual variables."
So if you have a set of x coordinates and y coordinates, it would look like this
x = rand(5,2);
y = rand(7,2);
d = pdist2(x,y)
d will be 5x7 matrix. d(i,j) will be the distance between x(i,:) and y(j,:).
okay I see what you mean cause i have read the section on pdist2 but i didn't understand that it worked with matrices as long as they are stored under a single variable name. alright that would allow me to get right of the NaN matrix along the diagonal in my elim_dist matrices.
Yes, but you may want to keep the NaNs. If you are calculating the min() and you have 0s, the 0s will always be the min (since you can't have negative distances). So you could use pdist2() or pdist() and replace the 0s on the diagonal with NaNs.
Hey Adam, if you have time could you please help me with this question I have asked in the link below. I wanted to go to you directly because you have the full code and understand the general idea of what im trying to do. I use the pdist2() function you've shown above to calculate the distances between the points in each matrix but I want to sort the existing HS based on which vein node they are closest too. So if a particular HS is closer to one vein node than all the other vein nodes then the hormone seed goes to a new matrix. Basically, after the sorting is done all the existing HS are sorted based on the vein node they are closest to into separate matricies. Thanks for all your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by