Trimming a curve - how to find nearest number and replace

2 次查看(过去 30 天)
I have the x and y coordinates for a bathymetric curve eg...
x y
0 1.36
0.03 -0.92
0.13 -1.68
1.69 -3.72
6.63 -3.35
9.18 -3.36
20.03 -3.26
31.75 -2.93
40.15 -1.70
46.21 0.10
69.97 2.26
I would like to trim this curve to a certain level line with at y=-0.15 and x from 0 to 45 I need to both replace the first coordinate with (0,-0.15) and make the final coordinate (45,-0.15) removing any x,y values greater than this.
I have this for a large number of data sets so of course a loop would be preferential but I cannot work out how to do these operations through code alone. The image below might clarify what I mean a little more, though it was made with different numbers.
Thanks in advance for any help

采纳的回答

Greg Dionne
Greg Dionne 2017-3-30
Try:
myLimit = -0.15;
iFirst = find(y<myLimit, 1, 'first');
iLast = find(y<myLimit, 1, 'last');
xdesired = x(iFirst:iLast);
ydesired = y(iFirst:iLast);
plot(x, y);
hold on
plot(xdesired, ydesired);
legend('Original','Trimmed')
  3 个评论
Greg Dionne
Greg Dionne 2017-3-31
OK. good.
For the concatenation I would probably do something like:
% use "," instead of ";" if xdesired and ydesired are row vectors
xdesired = [0; xdesired; 45];
ydesired = [mylimit; xdesired; mylimit];
Glad you got past this.
-G

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-3-30
The data you supplied does not look like your plots. But to clamp data to some level, try this:
xy = [...
0 1.36
0.03 -0.92
0.13 -1.68
1.69 -3.72
6.63 -3.35
9.18 -3.36
20.03 -3.26
31.75 -2.93
40.15 -1.70
46.21 0.10
69.97 2.26];
x = xy(:, 1);
y = xy(:, 2);
subplot(2, 1, 1);
plot(x, y, 'b-');
grid on;
yClamped = y; % Initialize
clampLevel = 0.15;
yClamped(y>clampLevel) = clampLevel; % Do the clamping
subplot(2, 1, 2);
plot(x, yClamped, 'b-');
grid on;
  1 个评论
Dom Smith
Dom Smith 2017-3-31
With a bit of modification I made the answer above work for my needs, but thanks so much for your help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by