Filter löschen
Filter löschen

Distances along a plotted line?

15 Ansichten (letzte 30 Tage)
Keith Lewis
Keith Lewis am 23 Dez. 2016
Kommentiert: Keith Lewis am 5 Jan. 2017
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!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Dez. 2016
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

Weitere Antworten (2)

Roger Stafford
Roger Stafford am 23 Dez. 2016
Bearbeitet: Roger Stafford am 23 Dez. 2016
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 am 23 Dez. 2016
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.

Community Treasure Hunt

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

Start Hunting!

Translated by