Trimming a curve - how to find nearest number and replace

조회 수: 8 (최근 30일)
Dom Smith
Dom Smith 2017년 3월 30일
댓글: Greg Dionne 2017년 3월 31일
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
Dom Smith
Dom Smith 2017년 3월 31일
With a bit of modification I've made this work thank you
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