how can i use a vector coordinate as input in function?

Hi guy, im tring to creat a function the calculate the distance between two point. The input are the coordinates of the vector like A=[x1 y1].I tried some thing like
function d = computeDistance(p1,p2)
p1=[x1,y1];
p2=[x2,y2];
d = sqrt((x1-x2)^2+(y1-y2)^2);
end
but it doesnt work at all. so can someone help me with this?
thanks

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 9 月 3 日

0 投票

You do not appear to have defined x1 or x2 but you use them.
Hint: any name that appears on the right hand side of an assignment must already exist.
Hint: to extract part of a vector, using indexing.
Andrei Bobrov
Andrei Bobrov 2016 年 9 月 3 日

0 投票

function d = computeDistance(p1,p2)
d = sqrt(sum((p1-p2).^2));
end

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2016 年 9 月 3 日

回答済み:

2016 年 9 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by