Calculating distances between coordinates from a matrix

24 views (last 30 days)
Hello users,
I am a new user of MATLAB and I am working on a final project for a class. This is my forst class using the app and I am at beginner level, so please bear with me ;) (Also, english is a second language, so please excuse any grammar mistakes)
Here is my question: I have a matrix wich represents individuals, along with their coordinates and other variables.
So far, my population matrix looks like this:
Pop = [1 1 2; 2 3 3; 3 5 1; 4 7 6; 5 8 2 ];
Where the first column is the ID of each individual, and columns 2 and 3 represent their location in X and Y respectively.
I am trying to make a distance matrix which would have the distances between each individual. I want a result that would look like this:
1 2 3
1 0 2.23 7.21
2 2.23 0 2.82
3 7.21 2.82 0
And so on for each individual (5 in that case)
Here is the code I have figured out so far:
Pop = [1 1 2; 2 3 3; 3 5 1; 4 7 6; 5 8 2 ]; %Col1=ID; Col2=positionX; Col3=PositionY
Dist = zeros (length(Pop(:,1))); %create distance matrix filled with zeroes
for j=1:length(Pop(:,1)) %go through each column
for i=1:length(Pop(:,1)) %go through each row
for t=1:length(Pop(:,1)); %do this loop the number of times we have people
Dist(i,j)=sqrt(((Pop(i,j+1)-Pop(i+1,j+1))^2)+(Pop(i,j+2)-Pop(i+1,j+2))^2) %euclidian distance
t+1 %iteration+1
end
end
end
Dist %show resulting matrix
The problem is that when it reaches the end of the lines of Pop, the are no more values so it stops. Also, for some reason, it does not calculate the distance between two same points as 0.
Thanks in advance for any help!
  1 Comment
Junaid Asmat
Junaid Asmat on 15 Nov 2017
I also want to write the code same as it is but I want to calculate distance between coordinates atleast 500. Want should I do?

Sign in to comment.

Accepted Answer

Chandra Kurniawan
Chandra Kurniawan on 20 Nov 2011
Pop = [1 1 2;
2 3 3;
3 5 1;
4 7 6;
5 8 2];
[m n] = size(Pop);
n = m;
Dist = zeros(m, n);
for i = 1 : m
for j = 1 : n
Dist(i, j) = sqrt((Pop(i, 1) - Pop(j, 1)) ^ 2 + ...
(Pop(i, 2) - Pop(j, 2)) ^ 2);
end
end
Dist
---------------------------------------------------------------- Dist =
0 2.2361 4.4721 6.7082 8.0623
2.2361 0 2.2361 4.4721 5.8310
4.4721 2.2361 0 2.2361 3.6056
6.7082 4.4721 2.2361 0 1.4142
8.0623 5.8310 3.6056 1.4142 0
>>
  1 Comment
Florian
Florian on 20 Nov 2011
Thanks, I noticed though that you calculate the distance using the first column of Pop as X, but that's just a detail and I figured it out.

Sign in to comment.

More Answers (3)

Andrei Bobrov
Andrei Bobrov on 20 Nov 2011
use function dist from Neural Network Toolbox
Dist = dist(Pop(:,2:3)')
variant 2 with use function pdist from Statistics Toolbox
Dist = tril(ones(5),-1)
Dist(logical(Dist))= pdist(Pop(:,2:3))
Dist = Dist + Dist.'
variant 3
Dist = sqrt(bsxfun(@minus,Pop(:,2),Pop(:,2)').^2+bsxfun(@minus,Pop(:,3),Pop(:,3)').^2);
variant 4
x = Pop(:,2);
y = Pop(:,3);
n = size(Pop,1);
K = nchoosek(1:n,2);
Dist = accumarray(K,hypot(diff(x(K),1,2),diff(y(K),1,2)),[n n]);
Dist = Dist + Dist.'

Alex
Alex on 20 Nov 2011
1. Your third loop over t is not needed. t isn't even used in your distance calculation.
2. your distance is measured wrong.
Dist(i,j) represents the distance between the ith & jth person.
So, it should be Dist(i,j) = sqrt( (xi - xj) ^2 + (yi - yj)^2);
Your current Dist is Dist(i,j) = sqrt( (xi - x(i+1))^2 + (yi - y(i+1)^2);
A simple test of would be to manually calculate Dist(1,1)
Using your current Dist()
Dist(1,1) = sqrt( (pop(1,2) - pop(2,2))^2 + (pop(1,3) - pop(2,3))^2)
Dist(1,1) = sqrt( ({x of person 1} - {x of person 2})^2 + ({y of person 1} - {y of person 2})^2)

Florian
Florian on 20 Nov 2011
Thank you everyone! I used a combination of Chandra's code, along with dist function suggested by Andrei.
Have a nice day all of you!

Categories

Find more on Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!