Azzera filtri
Azzera filtri

Distance from points to points.

5 visualizzazioni (ultimi 30 giorni)
Francesco
Francesco il 8 Feb 2014
Commentato: David il 8 Set 2015
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 Commento
Image Analyst
Image Analyst il 8 Feb 2014
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)?

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 8 Feb 2014
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 Commenti
Image Analyst
Image Analyst il 8 Feb 2014
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 il 8 Set 2015
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)

Accedi per commentare.

Più risposte (1)

Francesco
Francesco il 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 Commenti
Image Analyst
Image Analyst il 8 Feb 2014
Modificato: Image Analyst il 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.
Francesco
Francesco il 9 Feb 2014
Now I post another question where I speak better the question. If you want to answer I'm happy.

Accedi per commentare.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by