Distance from points to points.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
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)?
Risposta accettata
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
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
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)
Più risposte (1)
Francesco
il 8 Feb 2014
2 Commenti
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.
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!