How can I calculate distance between a point and a line in 4D (or space higer than 3D)?

Is there a function in MATLAB that calculates the shortest distance from a point to a line in N-Dimentional space?

 Accepted Answer

No, there's not, but you can calculate it using the dot product and the norm for any number of dimensions:
numDim = 4;
A = rand(numDim,1); %Point in line
B = rand(numDim,1); %Point in line
P = rand(numDim,1); %Point outside line
pa = P - A;
ba = B - A;
t = dot(pa, ba)/dot(ba, ba);
your_dist = norm(pa - t * ba,2)

More Answers (0)

Categories

Asked:

on 23 May 2013

Community Treasure Hunt

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

Start Hunting!