1
votes

I wish to check the distance between points (X2, Y2) and (X1, Y1) in each row. I used the code below but I do not get the right results (66 instead of 10). How can I set the pdist to calculate it correctly?

Script:

clc;
clear all;
x1 = [0;0;1;0;9;3;4;5;6;10;11;22];
y1 = [0;1;11;10;19;13;14;15;16;10;22;12];
 
x2 = [1;1;2;3;4;5;6;7;8;9;10;11];
y2 = [1;1;2;3;4;5;16;17;18;19;15;18];
 
dTable = table(x1,y1,x2,y2);
dArray = table2array(dTable);
D = pdist(dArray,'euclidean'); 
pdist compute the pairwise distance between pairs of measures as explained in the doc, also a table is not a valid input. But you can simply apply the euclidean distance equation on your arrays.obchardon
In the array they are already in pairs. So what is getting wrong there?alirazi
@alirazi In pdist, each row is an observation. It computes the distance from the first observation, row 1, to each of the other observations, rows 2 through n. Then it computes the distances between observation 2 and observations 3 through n, and so on. In your example, there are 12 observations, each one of which is a 4-dimensional point (not two 2-dimensional points). This is obviously not what you want.beaker
Thanks. It just seems easier to solve it with a loop: 'dx1 = dTable{i,1}; dy1 = dTable{i,2}; dx2 = dTable{i,3}; dy2 = dTable{i,4}; distpoints = sqrt((dx2-dx1)^2+(dy2-dy1)^2)'alirazi
D = sqrt( (x1 - x2).^2 + (y1 - y2).^2 )beaker