フィルターのクリア

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 件のコメント
Walter Roberson
Walter Roberson 2017 年 1 月 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 Center および File ExchangeSmoothing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by