필터 지우기
필터 지우기

Distances along a plotted line?

조회 수: 15 (최근 30일)
Keith Lewis
Keith Lewis 2016년 12월 23일
댓글: Keith Lewis 2017년 1월 5일
I have x and y matrices with y representing elevations at points x.
For instance
x = [0 5 10]
y = [2 4 1]
plot(x,y) gives me a cross section of elevations.
I want to mark various distances along the plotted line. For instance, place a marker every 1m along the line, or put individual markers at specific distances along the line.
How can I do this?
Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 23일
marker_dist = 1;
x = [0 5 10];
y = [2 4 1];
dist_from_start = cumsum( [0, sqrt((x(2:end)-x(1:end-1)).^2 + (y(2:end)-y(1:end-1)).^2)] );
marker_locs = marker_dist : marker_dist : dist_from_start(end); %replace with specific distances if desired
marker_indices = interp1( dist_from_start, 1 : length(dist_from_start), marker_locs);
marker_base_pos = floor(marker_indices);
weight_second = marker_indices - marker_base_pos;
marker_x = x(marker_base_pos) .* (1-weight_second) + x(marker_base_pos+1) .* weight_second;
marker_y = y(marker_base_pos) .* (1-weight_second) + y(marker_base_pos+1) .* weight_second;
plot(x, y);
hold on;
plot(marker_x, marker_y, 'r+');
hold off
  댓글 수: 2
Keith Lewis
Keith Lewis 2017년 1월 5일
Awesome thank you, this is great!

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

추가 답변 (2개)

Roger Stafford
Roger Stafford 2016년 12월 23일
편집: Roger Stafford 2016년 12월 23일
The approximate arclength along your curve from (x(i1),y(i1)) to (x(i2),y(i2)) can be computed as the sum of the line segment lengths connecting successive points between the two end points:
n = i2-i1;
s = sum(sqrt((x(i1+(1:n))-x(i1+(0:n-1))).^2-(y(i1+(1:n))-y(i1+(0:n-1))).^2));

Image Analyst
Image Analyst 2016년 12월 23일
For straight lines it's pretty trivial. For more general curves, see John D'Errico's interparc(): http://www.mathworks.com/matlabcentral/fileexchange/34874-interparc
Description
A common request is to interpolate a set of points at fixed distances along some curve in space (2 or more dimensions.) The user typically has a set of points along a curve, some of which are closely spaced, others not so close, and they wish to create a new set which is uniformly spaced along the same curve.
When the interpolation is assumed to be piecewise linear, this is easy. However, if the curve is to be a spline, perhaps interpolated as a function of chordal arclength between the points, this gets a bit more difficult. A nice trick is to formulate the problem in terms of differential equations that describe the path along the curve. Then the interpolation can be done using an ODE solver.
As an example of use, I'll pick a random set of points around a circle in the plane, then generate a new set of points that are equally spaced in terms of arc length along the curve, so around the perimeter of the circle.

카테고리

Help CenterFile Exchange에서 Smoothing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by