フィルターのクリア

How to find the distance between one slope and several points?

1 回表示 (過去 30 日間)
Beibit Sautbek
Beibit Sautbek 2016 年 7 月 21 日
回答済み: Azzi Abdelmalek 2016 年 7 月 21 日
I have slope AB, where
A=[x1,y1], B=[x2,y2], and point C=[x0,y0].
In order to find the distance between the point C and slope AB, I have used the formula below:
d=(abs(((x2-x1)*(y1-y0))-((x1-x0)*(y2-y1)))/(sqrt((x2-x1)^2+(y2-y1)^2)));
I can find the distance between slope and one point, but If I have several points, I have a problem in result.
For example:
I have a matrix A as below, where the first row represents A=[x1,y1], and the last row represents B=[x2,y2]:
A =
2 2
3 1
5 4
7 8
10 2
In matrix A, rows 2,3,4 represent 3 different points.
So I need to find a distance between slope and 3 points.
Actually, I did this code, but the result shows 4 values instead of 3 values.
l=length(A);
x1=A(1,1); y1=A(1,2);
x2=A(l,1); y2=A(l,2);
for i=2:l-1
x(i)=A(i,1);
y(i)=A(i,2);
d(i)=(abs(((x2-x1)*(y1-y(i))-((x1-x(i))*(y2-y1)))/(sqrt((x2-x1)^2+(y2-y1)^2))));
end
Could anyone check my code or help to get necessary answer?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 21 日
x0=A(2:4,1)
y0=A(2:4,2)
d=(abs(((x2-x1).*(y1-y0))-((x1-x0).*(y2-y1)))./(sqrt((x2-x1)^2+(y2-y1)^2)));

その他の回答 (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