A, B and Z define three points in the 3D *Euclidean* space of the form:
A = [x1;y1;0]; B = [x2;y2;0]; Z = [x2;y2;z];

Find the **Euclidean distance** between A and Z where

A = [1,0,0]; B = [5,3,0]; Z=[5,3,3];

>> euclidean(A,B,Z)

ans = 5.830951894845301

Your function should be able to handle 1 x 3 vectors or 3 x 1 vectors for all input parameters: A,B and Z. Z need not be 1 x 3 if A and B are. So 1x3,1x3,3x1 inputs, corresponding A, B and Z, are possible function input vectors.

HINT: use the Pythagorean formula.

6 Comments

Show
3 older comments

@bmtran
on 6 Feb 2012

what's the point of having 3 coordinates given? you're only asking for the distance between Z and A, right?

Bruce Raine
on 6 Feb 2012

You need x,y and z to to get the distance metric in 3D space. But you can reduce the problem to just 3 values by taking the differences between x values and y values and then just taking the 3rd element of z. So there is unnecessary padding in the vectors and you could reduce them all to 3 scalars. Essentially the problem boils down to: sqrt(x^2 + y^2 + z^2).

@bmtran
on 6 Feb 2012

looks like after you changed the test suite, it worked out anyway. thanks!

Bruce Raine
on 6 Feb 2012

Your solution is the best and yes I did alter/correct the test suite. Thanks.

Gary
on 6 Feb 2012

To repeat bmtran's question: Does B have anything to do with the problem?

Bruce Raine
on 8 Feb 2012

B is a red herring. I originally conceived the problem in 2D and then extended it to 3D. So you really only need A and Z vectors. However, B can still be used but this creates a more complex solution which you don't want. Sorry if you felt misled, this was my first attempt at trying to upload an interesting problem that was hopefully not too difficult to solve. :)

1 Comment

Alexander Wallar
on 6 Feb 2012

Cheater

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today