Calculating Distance from the origin to a Point P

5 ビュー (過去 30 日間)
Victoria
Victoria 2014 年 12 月 2 日
編集済み: Guillaume 2014 年 12 月 2 日
I was given this review question for my final exam in an introduction for engineering course. I am really struggling with the language (yes, I have searched all over the internet and asked for help from my professor and colleagues, I am really lost here).
The question asks us to write a function file to calculate the distance from the origin to point (x,y), then to call the function with input (x,y)= {(1,4),(2,5),(4,9),(-5,-3)}.
Here is my attempt:
>> z= [1 4;2 5;4 9;-5 -3]
z =
1 4
2 5
4 9
-5 -3
>> d=pdist(z,'euclidean')
d =
Columns 1 through 4
1.4142 5.8310 9.2195 4.4721
Columns 5 through 6
10.6301 15.0000
I know this is probably not right, but I don't know where I have gone wrong. Where does the origin come in to this?

採用された回答

Guillaume
Guillaume 2014 年 12 月 2 日
As per the documentation of pdist it calculates the euclidean distance between each pair of points in your matrix. Not what you want.
Use the standard mathematical formula to calculate your distance rather than looking for some built-in function, particularly since you're calculated the distance from the origin, this formula reduces to something very simple.
hint: what is?
sum(z.^2, 2)
  2 件のコメント
Victoria
Victoria 2014 年 12 月 2 日
My friend suggested this also, does it have the same issue as what I tried above?
>> x= [1 4;2 5;4 9;-5 -3]; >> y= [0 0]; >> d= pdist2(x,y)
d =
4.1231
5.3852
9.8489
5.8310
Guillaume
Guillaume 2014 年 12 月 2 日
編集済み: Guillaume 2014 年 12 月 2 日
Whether or not this gives the right result is probably irrelevant, as in all likelyhood, the purpose of the assignment is to see whether or not you know how to manipulate matrices, not whether you know how to call built-in functions.
Note: there's very little to add to the hint I gave you to get the right answer.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by