Using pdist with two matrix's

Hi,
So if I have one 102x2 matrix of x,y coordinates, and another 102x2 matrix of x,y coordinates, can pdist be used to compare all the rows in matrix 1 with the rows in matrix 2? As in for matrix one row 1, the x,y coordinates are compared to matrix 2 row 1 x,y coordinates; row 2 mat1 is compared to row 2 mat2 etc to find the distance between them, for all 102 rows?
I want to find their distances but I can't quite understand the help page on pdist, can it be used for this or is there another function I should look at?
Many thanks!

 Accepted Answer

Use pdist2()
distances = pdist2(xySet1, xySet2);

3 Comments

I tried this and got a 102x102 size matrix when I just want the distances between the first xy set and second xy set, like below
xy1 xy2 distances
1,2 1,4 2
1,3 1,3 0 etc
Which would be a 102x1 matrix, how can I get this? Thank you!
Then just use sqrt():
distances = sqrt((xy1(:, 1) - xy2(:, 1)) .^ 2 + (xy1(:, 2) - xy2(:, 2)) .^ 2)
Basically it's just distances = sqrt(deltax^2 + deltay^2)
Perfect, thank you so much!! Have a lovely day <3

Sign in to comment.

More Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Asked:

Mia
on 11 May 2020

Commented:

Mia
on 11 May 2020

Community Treasure Hunt

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

Start Hunting!