MATLAB: Euclidean distance for 3D data

3d datacalculateeuclidean distance

Hye, can anybody help me, what is the calculation to calculate euclidean distance for 3D data that has x,y and z value in Matlab? Thank you so much. Really appreciate if somebody can help me.

Best Answer

I guess, that you want the distance of a set of points to a specific other point:
% 100 points in 3D:
Pos = rand(100, 3);
% Euclidean distance to point [1,2,3]:
D = sqrt(sum((Pos - [1,2,3]).^2, 2))
With modern Matlab versions:
D = vecnorm(Pos - [1,2,3], 2, 2)
With old Matlab versions before the auto-expanding (< R2016):
V = bsxfun(@minus, Pos, [1,2,3]);
D = sqrt(sum(V.^2, 2))