Distance from points to points.

If I have this variable:
cord_new =
0.7000 0.4000
0.4500 0.3700
0.1000 0.6000
[righe, colonne]=size(cord_new)
gplot(ones(righe), cord_new, '-bs');
How can I plot the first value of cord_new with the second value of cord_new and the third value of cord_new. The important thing is that the first element is always fixed.

1 Comment

It's a 2D array, so exactly which element do you consider to be the third value? 0.1 (going down the row), or 0.45 (going across first, then down)?

Sign in to comment.

 Accepted Answer

Try this:
fontSize = 18;
cord_new =[...
0.7000 0.4000
0.4500 0.3700
0.1000 0.6000]
[rows, columns] = size(cord_new);
% Calculate distances of all points to first point.
distances = sqrt((cord_new(:,1) - cord_new(1,1)).^2 - (cord_new(:,2) - cord_new(1,2)).^2)
plot(distances, 'bs-', 'LineWidth', 2);
grid on;
title('Distances', 'FontSize', fontSize);
xlabel('Coordinate Number', 'FontSize', fontSize);
ylabel('Distance', 'FontSize', fontSize);

4 Comments

If you see the attached file you can see the situation. My goal is just to paint with a different color all the distances between the coordinates that are inside the variable cord_new. I have this graph on the screen.I just have to point out with a different color the lines. Is it possibile?
You have to consider the first coordinate of cord_new fixed. So I consider the distance of the first element to the second, the first to the third...and so on.
Well that's what I did: the distance of the first element to all the others. So that part is solved. You didn't plot compute or plot distances - you just plotted lines between the points. Are you now saying that you want distances in certain ranges to be certain colors? Like red if it's shorter than 3, blue if it's between 3 and 8, yellow if it's more than 8, or whatever? You can determine what color you want and then use that in plot. In the demo below, I just used random colors:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
cord_new =[...
0.7000 0.4000
0.4500 0.3700
0.1000 0.6000]
[rows, columns] = size(cord_new);
subplot(1,2,1);
for p1 = 1 : rows
text(cord_new(p1,1)+ 0.05, cord_new(p1,2), num2str(p1), 'FontSize', 20);
hold on;
for p2 = p1+1 : rows
% Calculate distances of all points to first point.
distances = sqrt((cord_new(p2,1) - cord_new(p1,1)).^2 - (cord_new(p2,2) - cord_new(p1,2)).^2)
% Plot line between the two points
x = [cord_new(p1,1), cord_new(p2,1)];
y = [cord_new(p1,2), cord_new(p2,2)];
plot(x, y, 'bs-', 'Color', rand(1,3), 'lineWidth', 2);
end
end
grid on;
title('Labeled Points', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
subplot(1,2,2);
% Calculate distances of all points to first point.
distances = sqrt((cord_new(:,1) - cord_new(1,1)).^2 - (cord_new(:,2) - cord_new(1,2)).^2)
bar(distances, 'BarWidth', 0.96);
grid on;
title('Distances', 'FontSize', fontSize);
xlabel('Coordinate Number', 'FontSize', fontSize);
ylabel('Distances from Point #1', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
to anyone using this code (notice the plus sign):
Image Analyst big fan of your help!
distances = sqrt((cord_new(:,1) - cord_new(1,1)).^2 + (cord_new(:,2) - cord_new(1,2)).^2)

Sign in to comment.

More Answers (1)

Francesco
Francesco on 8 Feb 2014
Your code is good but you have revolutionized the graphic display that I had previously on the screen. The bar plot is not important. In output I would like to have the previous graph (that you can see in the attached picture). The only difference is that the some distances (between coordinates) must be viewed in different ways: for example red.

2 Comments

Image Analyst
Image Analyst on 8 Feb 2014
Edited: Image Analyst on 8 Feb 2014
So get rid of the bar chart. I only did it because you said you want to "plot the distance of the first to the second, the first to the third and so" and so I plotted the distances like you asked at first. Distance is computer via the Pythagorean theorem - not sure if you have a different definition than the rest of us. Maybe you meant "path" or "connecting line"??? If you don't want the distances anymore and only want the connecting lines, then get rid of all subplots and the call to bar and its labels and titles. And like I said, you can use whatever colors you want. You don't have to use rand(1,3) to get a random color like I did.
Now I post another question where I speak better the question. If you want to answer I'm happy.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 8 Feb 2014

Commented:

on 8 Sep 2015

Community Treasure Hunt

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

Start Hunting!