Distance from points to points.

5 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2014-2-8
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)?

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2014-2-8
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 个评论
Image Analyst
Image Analyst 2014-2-8
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')
David
David 2015-9-8
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)

请先登录,再进行评论。

更多回答(1 个)

Francesco
Francesco 2014-2-8
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 个评论
Image Analyst
Image Analyst 2014-2-8
编辑:Image Analyst 2014-2-8
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.
Francesco
Francesco 2014-2-9
Now I post another question where I speak better the question. If you want to answer I'm happy.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by