finding the distance between two points

26 views (last 30 days)
Natali Petani
Natali Petani on 12 Nov 2022
Edited: Karim on 12 Nov 2022
prompt = "enter the number of elements to make";
x = input(prompt);
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
M = 2;
N = x/M;
r = 1;
R = 2;
W = linspace(r,R,M);
C = linspace(0,2*pi,N);
[R,T] = meshgrid(W,C);
X = R.*cos(T);
Y = R.*sin(T);
[m,n] = size(X)
figure
set(gcf, 'color', 'w');
axis equal;
axis on;
hold on;
for i = 1:m
plot(X(i,:), Y(i,:), '-o');
end
for j=1:n
G = plot(X(:,j),Y(:,j), 'b', 'linewidth', 1);
end
fprintf('the x coordinates of the nodes are')
X
fprintf('the y coordinates of the nodes are')
Y
%distance b/w two nodes
How do I find the distance between two nodes? I need to find the length of each side of the sements created (4 lengths in total).

Answers (3)

Sam Chak
Sam Chak on 12 Nov 2022
You can try the function: pdist2().
help pdist2
PDIST2 Pairwise distance between two sets of observations. D = PDIST2(X,Y) returns a matrix D containing the Euclidean distances between each pair of observations in the MX-by-N data matrix X and MY-by-N data matrix Y. Rows of X and Y correspond to observations, and columns correspond to variables. D is an MX-by-MY matrix, with the (I,J) entry equal to distance between observation I in X and observation J in Y. D = PDIST2(X,Y,DISTANCE) computes D using DISTANCE. Choices are: 'euclidean' - Euclidean distance (default) 'squaredeuclidean' - Squared Euclidean distance 'seuclidean' - Standardized Euclidean distance. Each coordinate difference between rows in X and Y is scaled by dividing by the corresponding element of the standard deviation computed from X, S=NANSTD(X). To specify another value for S, use D = PDIST2(X,Y,'seuclidean',S). 'cityblock' - City Block distance 'minkowski' - Minkowski distance. The default exponent is 2. To specify a different exponent, use D = PDIST2(X,Y,'minkowski',P), where the exponent P is a scalar positive value. 'chebychev' - Chebychev distance (maximum coordinate difference) 'mahalanobis' - Mahalanobis distance, using the sample covariance of X as computed by NANCOV. To compute the distance with a different covariance, use D = PDIST2(X,Y,'mahalanobis',C), where the matrix C is symmetric and positive definite. 'cosine' - One minus the cosine of the included angle between observations (treated as vectors) 'correlation' - One minus the sample linear correlation between observations (treated as sequences of values). 'spearman' - One minus the sample Spearman's rank correlation between observations (treated as sequences of values) 'hamming' - Hamming distance, percentage of coordinates that differ 'jaccard' - One minus the Jaccard coefficient, the percentage of nonzero coordinates that differ function - A distance function specified using @, for example @DISTFUN A distance function must be of the form function D2 = DISTFUN(ZI,ZJ), taking as arguments a 1-by-N vector ZI containing a single observation from X or Y, an M2-by-N matrix ZJ containing multiple observations from X or Y, and returning an M2-by-1 vector of distances D2, whose Jth element is the distance between the observations ZI and ZJ(J,:). For built-in distance metrics, the distance between observation I in X and observation J in Y will be NaN if observation I in X or observation J in Y contains NaNs. D = PDIST2(X,Y,DISTANCE,'Smallest',K) returns a K-by-MY matrix D containing the K smallest pairwise distances to observations in X for each observation in Y. PDIST2 sorts the distances in each column of D in ascending order. D = PDIST2(X,Y,DISTANCE, 'Largest',K) returns the K largest pairwise distances sorted in descending order. If K is greater than MX, PDIST2 returns an MX-by-MY distance matrix. For each observation in Y, PDIST2 finds the K smallest or largest distances by computing and comparing the distance values to all the observations in X. [D,I] = PDIST2(X,Y,DISTANCE,'Smallest',K) returns a K-by-MY matrix I containing indices of the observations in X corresponding to the K smallest pairwise distances in D. [D,I] = PDIST2(X,Y,DISTANCE, 'Largest',K) returns indices corresponding to the K largest pairwise distances. Example: % Compute the ordinary Euclidean distance X = randn(100, 5); Y = randn(25, 5); D = pdist2(X,Y,'euclidean'); % euclidean distance % Compute the Euclidean distance with each coordinate difference % scaled by the standard deviation Dstd = pdist2(X,Y,'seuclidean'); % Use a function handle to compute a distance that weights each % coordinate contribution differently. Wgts = [.1 .3 .3 .2 .1]; % coordinate weights weuc = @(XI,XJ,W)(sqrt((XI - XJ).^2 * W')); Dwgt = pdist2(X,Y, @(Xi,Xj) weuc(Xi,Xj,Wgts)); See also PDIST, KNNSEARCH, CREATENS, KDTreeSearcher, ExhaustiveSearcher. Documentation for pdist2 doc pdist2 Other uses of pdist2 gpuArray/pdist2 tall/pdist2

Star Strider
Star Strider on 12 Nov 2022
I am not exactly certain what you want.
Calculating the perimeters of each segment appears to be straightforward —
% prompt = "enter the number of elements to make";
% x = input(prompt);
x = 16;
M = 2;
N = x/M;
r = 1;
R = 2;
W = linspace(r,R,M);
C = linspace(0,2*pi,N);
[R,T] = meshgrid(W,C);
X = R.*cos(T);
Y = R.*sin(T);
[m,n] = size(X)
m = 8
n = 2
figure
set(gcf, 'color', 'w');
axis equal;
axis on;
hold on;
for i = 1:m
plot(X(i,:), Y(i,:), '-o');
end
for j=1:n
G = plot(X(:,j),Y(:,j), 'b', 'linewidth', 1);
end
fprintf('the x coordinates of the nodes are')
the x coordinates of the nodes are
X
X = 8×2
1.0000 2.0000 0.6235 1.2470 -0.2225 -0.4450 -0.9010 -1.8019 -0.9010 -1.8019 -0.2225 -0.4450 0.6235 1.2470 1.0000 2.0000
fprintf('the y coordinates of the nodes are')
the y coordinates of the nodes are
Y
Y = 8×2
0 0 0.7818 1.5637 0.9749 1.9499 0.4339 0.8678 -0.4339 -0.8678 -0.9749 -1.9499 -0.7818 -1.5637 -0.0000 -0.0000
%distance b/w two nodes
dX = hypot(diff(X,[],2), diff(Y,[],2)); % Radial Distances
dY = hypot(diff(X), diff(Y)); % Circumferential Distances
dY = [dY(1,:); dY]; % Duplicate First Row
Perimeters = sum([2*dX dY],2) % Perimeters Of Each Segment
Perimeters = 8×1
4.6033 4.6033 4.6033 4.6033 4.6033 4.6033 4.6033 4.6033
.

Karim
Karim on 12 Nov 2022
Edited: Karim on 12 Nov 2022
To identify the distances between the nodes of a segment you first need to identify the nodes that make up that segment. Once you have those coordinates the distance can be determined. See below for an example. I adjusted the figure a bit to highlight the segment and the node numbers.
x = 16; % user input
M = 2; % number or radii divisions
N = x/M; % number of angluar divisions
r = 1; % bound inner radius
R = 2; % bound outer radius
W = linspace(r,R,M); % radii for the plot
C = linspace(0,2*pi,N); % angluar segments for the plot
[R,T] = meshgrid(W,C); % combined radii (R) and angle segment (T)
X = R.*cos(T); % x coordinates for the points
Y = R.*sin(T); % y coordinates for the points
[m,n] = size(X);
% reshape the points to ease the plotting
% note that we add 'nan' to disconnect the different lines
X_circ = reshape([X;NaN(1,n)],[],1);
Y_circ = reshape([Y;NaN(1,n)],[],1);
X_seg = reshape([X,NaN(m,1)]',[],1);
Y_seg = reshape([Y,NaN(m,1)]',[],1);
% create the grid for the segment highlight
SegGrid = [X(1,1) Y(1,1);
X(1,2) Y(1,2);
X(2,2) Y(2,2);
X(2,1) Y(2,1)];
% evaluate the distance
Edge_12 = sqrt( (SegGrid(1,1)-SegGrid(2,1))^2 + (SegGrid(1,2)-SegGrid(2,2))^2);
Edge_23 = sqrt( (SegGrid(2,1)-SegGrid(3,1))^2 + (SegGrid(2,2)-SegGrid(3,2))^2);
Edge_34 = sqrt( (SegGrid(3,1)-SegGrid(4,1))^2 + (SegGrid(3,2)-SegGrid(4,2))^2);
Edge_41 = sqrt( (SegGrid(4,1)-SegGrid(1,1))^2 + (SegGrid(4,2)-SegGrid(1,2))^2);
figure
hold on
% plot the circles
plot(X_circ,Y_circ,'b','linewidth',1);
% plot the inner edges
plot(X_seg ,Y_seg ,'r','Marker','o');
% highlight the first segment in yellow
patch('Faces',[1 2 3 4],'Vertices',SegGrid,'FaceColor','y','FaceAlpha',0.5,'EdgeColor','none')
% print text to indicate the node number
text(SegGrid(:,1),SegGrid(:,2)," "+num2str((1:4)'))
hold off
axis equal
grid on
fprintf('Edge between nodes %i and %i has length %.3f \n',1,2,Edge_12)
Edge between nodes 1 and 2 has length 1.000
fprintf('Edge between nodes %i and %i has length %.3f \n',2,3,Edge_23)
Edge between nodes 2 and 3 has length 1.736
fprintf('Edge between nodes %i and %i has length %.3f \n',3,4,Edge_34)
Edge between nodes 3 and 4 has length 1.000
fprintf('Edge between nodes %i and %i has length %.3f \n',4,1,Edge_41)
Edge between nodes 4 and 1 has length 0.868
% EDIT: added the perimeter
SegPer = Edge_12+Edge_23+Edge_34+Edge_41;
fprintf('Perimeter of the segment is %.3f \n',SegPer)
Perimeter of the segment is 4.603

Community Treasure Hunt

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

Start Hunting!

Translated by