Calculating distances between coordinates from a matrix
21 views (last 30 days)
Show older comments
Florian
on 20 Nov 2011
Commented: Junaid Asmat
on 15 Nov 2017
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
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?
Accepted Answer
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
>>
More Answers (3)
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.'
0 Comments
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)
0 Comments
See Also
Categories
Find more on Conway's Game of Life 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!