Trimming a curve - how to find nearest number and replace
8 次查看(过去 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
0 个评论
采纳的回答
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
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
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;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!