# How to measure the shortest distance among different points using Matlab

11 views (last 30 days)
M.S. Khan on 27 Oct 2020
Commented: M.S. Khan on 29 Oct 2020 at 4:15
Lets suppoese i have the points as shown by B. For example, P1(1,1,2 ), P2(1,2,3 ) etc as shown by B. I want to measure the shortest distance among these points.
x = B(:,1), y =B(:,2) and z = B(:,3).
Thanks in advance for support from community members. Regards!
B =
1 1 3
1 2 3
2 3 1
2 3 2
3 1 1
3 3 1
3 3 2

M.S. Khan on 28 Oct 2020
Thanks Sudkhakr Shinde and KSSV for your kind reply. Actually, we can use 'norm' function to measure distance between two points. But i want to measure the distance among all the points, then i want to pick the minimum distance. Please any help. Regards
KSSV on 28 Oct 2020
That's what pdist does....did you read the documentation?
M.S. Khan on 28 Oct 2020
Dear KSSV, i am trying to understand its documentation and then will apply. Thanks for your guidance. Regards!

Jakob on 28 Oct 2020
B = randi([-10 10],5,3); % just some random numbers
minimum = sqrt(sum((B(1,:)- B(2,:)).^2));
for i = 1 : size(B,1)-1
for j = i+1 : size(B,1)
minimum = min(minimum,sqrt(sum((B(i,:)- B(j,:)).^2)));
end
end
disp(minimum)

Jakob on 28 Oct 2020
But thats basically pdist lol
M.S. Khan on 29 Oct 2020 at 4:15
Thanks KSSV, pdist() functions works. Thanks for reply Dear Jakob. your code and pdist() function works same.