필터 지우기
필터 지우기

how to find the distance?

조회 수: 6 (최근 30일)
bsd
bsd 2011년 9월 18일
Hai,
I have drawn a line between two points, I need to find the length(distance) of that line between those two points. Is there a command in matlab that could find the length(distance) of the line? Looking forward to hearing from you soon.
BSD

답변 (2개)

Jan
Jan 2011년 9월 18일
point1 = rand(1, 3);
point2 = rand(1, 3);
distance = norm(point1 - point2);
Or:
d = point1 - point2;
distance = sqrt(d * d');
Or with the fast C-Mex FEX: DNorm2, which operates on rows or columns of matrices also:
distance = DNorm2(point1 - point2);
  댓글 수: 2
Michael Leung
Michael Leung 2011년 9월 19일
bsd, I'm not sure in how many dimensions you are working in, but in 2D, and 3D, norm would be the function you are looking for.
It uses simple Pythagorean theorem to find the distance.
(I'm not sure if Pythagorean would apply in higher dimensions, I can not visualize how it would work! XD)
Jan
Jan 2011년 9월 19일
The 2-norm (Euclidean norm) works in arbitrary dimensions. It is: SQRT(SUM(x.^2)). In my example SUM(x.^2) is implemented faster using the DOT product x*x', which is calculated by the fast BLAS library.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 9월 18일
Use hypot
distance = hypot(point1 - point2);
This assumes, of course, that the line is a straight one and the measurement is in Euclidean geometry, and the distance metric is the Euclidean distance.
  댓글 수: 2
Jan
Jan 2011년 9월 18일
@Walter: The documentation you've linked to explains, that HYPOT needs two inputs. HYPOT(point1, point2) does also not measure the distance.
Walter Roberson
Walter Roberson 2011년 9월 19일
Yes, you are right. But if you are working in 2d, then splitting the difference in two a and b components and using hypot() will generate more precise answers if the differences might be quite small, especially below sqrt(eps(0)) (approximately 1E-162)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by