Calculating distance between 3 points.

9 ビュー (過去 30 日間)
gblmtc
gblmtc 2018 年 5 月 16 日
回答済み: gblmtc 2018 年 5 月 17 日
Hi there!
I have 3 matrix, each one has the first column the X coordinate, the second column the y coordinate and the 3rd column is the z coordinate. What I have to obtain is the distances between P and D and the distance between P and A in order to make a plot and to present the differences.
P = [2 3 6 ; 6 94 20 ; 9 6 3 ; 6 3 2];
D = [5 6 9 ; 8 3 20 ; 9 8 7 ; 6 5 8];
A = [5 2 3 ; 8 9 4 ; 7 5 2 ; 9 8 5];
xi = P(:,1);
yi = P(:,2);
zi = P(:,3);
xj = D(:,1);
yj = D(:,2);
zj = D(:,3);
dist = sqrt((xi-xj).^2 + (yi-yj).^2 + (zi-zj).^2); % calculate dist between P and D
xj = A(:,1);
yj = A(:,2);
zj = A(:,3);
dist = sqrt((xi-xj).^2 + (yi-yj).^2 + (zi-zj).^2); % now it calculate dist between P and A
My teacher actually asked me to make a function which can calculate both distances and not to make 2 different formulas ( what I wrote in the code makes no sense :( ). I was thinking to use for loop in order to define xj,yj and zj because xi,yi and zi are always the same but I don't know how could I do that. Could you please give me a hint? Thanks!

採用された回答

sloppydisk
sloppydisk 2018 年 5 月 16 日
You can just use vecnorm, right?
PD = vecnorm(P-D, 2, 2);
PA = vecnorm(P-A, 2, 2);
  2 件のコメント
gblmtc
gblmtc 2018 年 5 月 17 日
Thanks sloppydisk :) Yes, it works! The teacher said to use the formula for distance because that's what we were studying. I'll ask him anyway! Thanks again!
sloppydisk
sloppydisk 2018 年 5 月 17 日
編集済み: sloppydisk 2018 年 5 月 17 日
Ah I think he wants you to write a function like vecnorm yourself. Check out how to do that by reading this link. You can use code similar to what you wrote, except wrapped inside a function getting two nx3 matrices as input.

サインインしてコメントする。

その他の回答 (1 件)

gblmtc
gblmtc 2018 年 5 月 17 日
Yes, he wants me to think a function that calculate the both distances. Actually he wants me to figure out how to define xj, yj and zj in order to be ones the D matrix and after that to be A matrix. Thanks for your help!

カテゴリ

Help Center および File ExchangeOctave についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by