Finding the first occurance using interp1

Hello. I have some data (red curve) and I'm trying to find the X value at which Y = 0.2.
I have used
Y20 = interp1(Y,X,0.2,'linear')
which works well, but finds the last occurrance.
How can I find the first occurrance (i.e. around x=9)
Thanks

 채택된 답변

Matt J
Matt J 2018년 2월 27일
편집: Matt J 2018년 2월 27일

0 개 추천

Use only the first two data X,Y data points in the interpolation.

댓글 수: 6

Jason
Jason 2018년 2월 27일
Hi, but its not always the 1st two data points. sometime my real data is very similar to the black curve.
Then go through your data from the beginning, note when the Y-value is first greater or equal to 0.2 (say at position I in the Y-array) and then call interp1 as
Y20 = interp1(Y(1:I),X(1:I),0.2,'linear')
Best wishes
Torsten.
Jason
Jason 2018년 2월 27일
편집: Jason 2018년 2월 27일
Like this:
[~,I] = min(abs(Y - 0.2))
i=find(Y(1:end-1)>=0.2 & Y(2:end)<=0.2,1);
Y20=interp1(Y(i:i+1),X(i:i+1),0.2);
Torsten
Torsten 2018년 2월 27일
It could happen that Y is increasing, couldn't it ?
Matt J
Matt J 2018년 2월 27일
편집: Matt J 2018년 2월 27일
Not according to the posted figure, but even if it could, I think the extension is an exercise I'll leave for the OP.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2018년 2월 27일
편집: Sean de Wolski 2018년 2월 27일

0 개 추천

Use cummax and cummin to find the the first set of points that cross 0.2. Then interp just them.
x = 1:10
y = sin(x)
plot(x,y)
yval = 0.2;
idx = find(cummin(y)<0.2 & cummax(y)>0.2, 1, 'first')
interp1(y([idx-1 idx]), x([idx-1, idx]), 0.2)

댓글 수: 1

Jason
Jason 2018년 2월 27일
Thankyou for your answer. Im sorry I can't accept both. Matt came first.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2018년 2월 27일

편집:

2018년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by